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

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
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
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54260 488
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49266 226
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement