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:!("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! or more generally, try reading the docs.


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:

|> Jaxon.Stream.from_enumerable()
|> Jaxon.Stream.query([:root, "users", :all, "metadata"])
|> Stream.into(!("large_file.csv"))

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:


and here is a code:

|> 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

|> Jason.decode!()
|> Enum.random()