array json streaming inside json object

106 views
Skip to first unread message

Jérôme LAFORGE

unread,
Feb 19, 2020, 8:15:36 AM2/19/20
to golang-nuts
Hello,
Do you know an elegant, simple and clean way to stream an json array inside json object?

Here an example, that does the job, but I find it complicated (and how we manage the comma when document has error and we have to ignore it)


thx in adv
BR
Jérôme

Amnon Baron Cohen

unread,
Feb 20, 2020, 1:04:40 PM2/20/20
to golang-nuts
Unfortunately there is not a more elegant way.

Unless you change the protocol, and dispense with the idea of returning a single json object,
and instead return line delimited streaming json.
Then you can dispense with injecting brackets into your stream,
and just call 

  enc.Encode(doc)
  w.Write([]byte('\n'))

for each doc

Brian Candler

unread,
Feb 20, 2020, 3:06:21 PM2/20/20
to golang-nuts
There is even a "standard" for this: http://jsonlines.org/

Jérôme LAFORGE

unread,
Feb 21, 2020, 12:06:58 AM2/21/20
to golang-nuts
Thank you both.

When I am the designer of API that must stream json array, I use JSONL. And it is much easier to use it as sender.

Unfortunately, in this case, I can't modify the API.

I read somewhere (I can't remember where), that somebody plans to work on simplicity for streaming json object with Go
Does someone know if it is actually the case or if it still plans?

Amnon Baron Cohen

unread,
Feb 21, 2020, 2:33:02 AM2/21/20
to golang-nuts
https://github.com/golang/go/issues/33714 is related.

The problem you describe is far greater when decoding.
Reply all
Reply to author
Forward
0 new messages