spizzy
August 8, 2022, 9:01am
1
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).
spizzy
August 8, 2022, 9:20am
3
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})
1 Like
Sebb
August 8, 2022, 9:32am
4
decode! is not happy with File.Stream.t
as input. Try File.read! or more generally, try reading the docs.
4 Likes
Eiji
August 8, 2022, 10:00am
5
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()
1 Like
spizzy
August 8, 2022, 10:23am
6
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?
Eiji
August 8, 2022, 10:53am
7
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.
spizzy
August 8, 2022, 11:18am
8
What’s the output of that?
Eiji
August 8, 2022, 11:22am
9
A random Elixir
string like "First"
.
Sebb
August 8, 2022, 11:37am
10
I’d not recommend this solution for a beginner, rather just
"names.json"
|> File.read!()
|> Jason.decode!()
|> Enum.random()
1 Like
If I do Jason.decode(File.read('names.json')
, I get that error
1st argument: not an iodata term
. The json is in this format
{:ok, "{\n \"key\": \"value\"\n}"}
with a weird :ok
that I don’t know how to deal with.
But your syntax works. I don’t get it
Sebb
May 2, 2023, 10:21pm
13
you forgot the !
. Without it those functions return an ok-tuple, with they raise.
2 Likes