Format content from JSON file?

I want to get rid of the strings "" being displayed from the Enum.random()
like: " \"Dislike_YouTube\",\n" => Dislike_YouTube

I tried to do Poison.decode!() but its erroring:

File.stream!("names.json")
|> Enum.random()
|> Poison.decode!()

** (Poison.ParseError) unexpected token at position 21: ,

Picking random line from JSON file do not guarantee that the returned string will be proper JSON string. First decode, then pick random (if you can).

ye it tried to move the decode above the random actually but then got:

** (ArgumentError) errors were found at the given arguments:

  * 1st argument: not an iodata term

    :erlang.iolist_to_binary(%File.Stream{line_or_bytes: :line, modes: [:raw, :read_ahead, :binary], path: "names.json", raw: true})

decode! is not happy with File.Stream.t as input. Try File.read! or more generally, try reading the docs.

4 Likes

If you have a huge JSON file then you may consider using jaxon library instead.

Take a look at example from it’s readme file:

"large_file.json"
|> File.stream!()
|> Jaxon.Stream.from_enumerable()
|> Jaxon.Stream.query([:root, "users", :all, "metadata"])
|> Stream.map(&(&1["username"],",",&1["email"],"\n"))
|> Stream.into(File.stream!("large_file.csv"))
|> Stream.run()

well its around 5k lines and what I want to do is to return a random name like
Mad_Player but I always get "Mad_Player" back, is that a problem?

Here is example names.json file content:

[
  "First",
  "Second",
  "Third"
]

and here is a code:

"names.json"
|> File.stream!()
|> Jaxon.Stream.from_enumerable()
|> Jaxon.Stream.query([:root, :all])
|> Enum.random()

As long as it’s a valid json file the format does not matter. Each name can be in separate line as well as in one line.

What’s the output of that?

A random Elixir string like "First".

I’d not recommend this solution for a beginner, rather just

"names.json"
|> File.read!()
|> Jason.decode!()
|> Enum.random()

Thanks