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.

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.

Where Next?

Popular in Questions Top

stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
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
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement