spizzy

spizzy

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

Marked As Solved

Sebb

Sebb

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

Also Liked

Sebb

Sebb

you forgot the !. Without it those functions return an ok-tuple, with they raise.

spizzy

spizzy

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})
Eiji

Eiji

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

Last Post!

Sebb

Sebb

you forgot the !. Without it those functions return an ok-tuple, with they raise.

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44167 214
New

We're in Beta

About us Mission Statement