stuartc

stuartc

Generating JSON responses as a stream

I’m trying to generate a stream of JSON from an Ecto stream to be sent out via send_chunked/2 and chunk/2.

It appears that all the JSON libraries I can find are only for parsing.
With the exception of GitHub - TreyE/json_stream_encoder: Streaming encoder for JSON in elixir. · GitHub

I got stuck trying to use this library since it’s built around IO.binwrite which needs an IO device; I’m more comfortable with streams at the moment.

The gist of what I need is IO’ish device or a Stream of strings, but gives me the opportunity to format things as I go.

Individual items in the response aren’t big on their own, but there could be thousands of them.

Is there any library or approach anyone can share?

Marked As Solved

LostKobrakai

LostKobrakai

Ecto streams are not arbitrary, but essentially chunks. You always get a fixed(max) number of full rows, never partial rows. So your stream could essentially be:

rows = Stream.map(ecto_stream, &Jason.encode!/1)
Stream.concat([["["], Stream.intersperse(rows, ","), ["]"]])

Or switch to ndjson and just join the rows with newlines.

Also Liked

krasenyp

krasenyp

They might even query records as JSON, if the database supports it, and skip encoding in the application altogether.

Last Post!

LostKobrakai

LostKobrakai

Yeah. Stream vs Enum APIs is about the output, not the input. The inputs are usually similar / the same and accept any Enumerables.

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement