Live data from Hacker News

JSON River – Parse JSON incrementally as it streams in

github.com

71–80 of 101 posts

Re: JSON River – Parse JSON incrementally as it streams in

#71
post #38

Oh fun, I wrote a similar library in 2015 for Haskell. There is an annoying gotcha to deal with: there are sequences of valid characters that can be parsed incorrectly if you’re doing incremental chunks, namely if “0.0” is split across two input chunks you can get a token stream with two valid float literals rather than 1! Namely “0” and “.0”, which is just a really annoying wart of json float syntax.

Don't you need to wait for some kind of delimiter (like ",", "]", "}", newline, EOF) before parsing something else than a string?

Only for numbers! Strings, objects, arrays, true, false, and null all have an unambiguous ending.

Re: JSON River – Parse JSON incrementally as it streams in

#72
post #66
post #35

Hi HN! Didn't expect this to be on the front page today! I should really release all the optimizations that've been landing lately, the version on github is about twice as fast as what's released on npm. I wrote it when I was doing prototyping on doing streaming rendering of UIs defined by JSON generated by LLMs. Using constrained generation you can essentially hand the model a JSON serializable type, and it will alw…

I've just published v1.0.1. It's about 2x faster, and should have no other observable changes. The speedup is mainly from avoiding allocation and string slicing as much as possible, plus an internal refactor to bind the parser and tokenizer more tightly together. Previously the parser would get an array of tokens each time it pushed data into the tokenizer. This was easy to write, but it meant we needed to allocate t…

I want to ditch stream-json so hard (needs polyfills in browser, cumbersome to use), but I need only one feature: invoke callback by path (e.g. `user.posts` need to invoke for each post in array) only for complete objects. Is this something that json river can support?

Re: JSON River – Parse JSON incrementally as it streams in

#73
post #49

Earlier quoted context omitted.

Doesn't it do exactly that? > As a consequence of 1 and 5, we only add a property to an object once we have the entire key and enough of the value to know that value's type.

Their example in the README is extremely misleading then. It indicates your stream output is name: A name: Al name: Ale name: Alex Which would suggest you are getting unfinished strings out in the stream.

How is it misleading? It shows that it gives back unfinished values but finished keys.

Re: JSON River – Parse JSON incrementally as it streams in

#74
post #16
post #13

Earlier quoted context omitted.

If you're building a UI that renders output from a streaming LLM you might get back something which looks like this: {"role": "assistant", "text": "Here's that Python code you aske Incomplete parsing with incomplete strings is still useful in order to render that to your end user while it's still streaming in.

incomplete strings could be fun in certain cases {"cleanup_cmd":"rm -rf /home/foo/.tmp" }

Incremental JSON parsing is key for LLM apps, but safe progressive UIs also need to track incompleteness and per-chunk diffs. LangDiff [1] would help with that.

[1]: https://github.com/globalaiplatform/langdiff/tree/main/ts

Re: JSON River – Parse JSON incrementally as it streams in

#75
post #49

Earlier quoted context omitted.

Doesn't it do exactly that? > As a consequence of 1 and 5, we only add a property to an object once we have the entire key and enough of the value to know that value's type.

Their example in the README is extremely misleading then. It indicates your stream output is name: A name: Al name: Ale name: Alex Which would suggest you are getting unfinished strings out in the stream.

[deleted]

Re: JSON River – Parse JSON incrementally as it streams in

#77
post #72
post #66

Earlier quoted context omitted.

I've just published v1.0.1. It's about 2x faster, and should have no other observable changes. The speedup is mainly from avoiding allocation and string slicing as much as possible, plus an internal refactor to bind the parser and tokenizer more tightly together. Previously the parser would get an array of tokens each time it pushed data into the tokenizer. This was easy to write, but it meant we needed to allocate t…

I want to ditch stream-json so hard (needs polyfills in browser, cumbersome to use), but I need only one feature: invoke callback by path (e.g. `user.posts` need to invoke for each post in array) only for complete objects. Is this something that json river can support?

jsonriver's invariants do give you enough info to notice which values are and aren't complete. They also mean that you can mutate the objects and arrays it returns to drop data that you don't care about.

There might be room for some helper functions in something like a 'jsonriver/helpers.js' module. I'll poke around at it.

Re: JSON River – Parse JSON incrementally as it streams in

#78
post #76

This looks really nice. I think I could find a use for this. The title made me think of Star Trek DS9 and Nog talking about The Great Material Continuum. “Nog: The river will provide”

“Oh, that river. It can be very treacherous.” - Rom

Re: JSON River – Parse JSON incrementally as it streams in

#79
I couldn’t find a library like this in PHP, but realized for my use case I could easily hack something together. Algorithm is simply:

- trim off all trailing delimiters: },"

- then add on a fixed suffix: "]}

- then try parsing as a standard json. Ignore results if fails to parse.

This works since the schema I’m parsing had a fairly simple structure where everything of interest was at a specific depth in the hierarchy and values were all strings.

Re: JSON River – Parse JSON incrementally as it streams in

#80
I wrote something similar in my last job where we had to parse and query data from huge (50+ GB? I remember they weren't even fitting in my laptop) json files that were stored in an S3 Bucket..

We used the streaming parser to create an index of the file locally {json key: (byte offset, byte size)} and then simply used http range queries to access the data we needed.

Here is the full write up about it:

https://dinesh.cloud/2022/streaming-json-for-fun-and-profit/

And here is the open sourced code:

https://github.com/multiversal-ventures/json-buffet

Post reply on HN