Json parser for escaped json?

8 views
Skip to first unread message

Ken Hancock

unread,
Jun 9, 2022, 8:58:48 PM6/9/22
to jackson-user
I have a very large (> JVM memory) payload that requires the extraction of certain fields for processing.  The twist is the fields have escaped json as the payload. Does Jackson have any capability of parsing this as a stream?

Example:
{
  "field1":"value1",
  "field2":"value2",
  "verylargedatablock": "{\"large1\":\"value1\",\"large2\":\"value2\",\"bigarray\":[{\"foo\":\"bar\"}]}"
}


I need to be able to  parse out fields from "verylargedatablock" without loading the entire value into memory in order to deserialize it.

Tatu Saloranta

unread,
Jun 9, 2022, 9:06:23 PM6/9/22
to jackson-user
There is one way to stream String values, by using JsonParser method

getText(Writer w)

which may handle decoding incrementally: implementations may or may
not buffer content.
But for JSON parser in particular I think actual streaming is in fact
implemented. So you would need to construct a JsonParser and iterate
over tokens, then call this method with a `Writer` you implement to
process content.

But the problem then becomes that of how to parse content passed via Writer.

This is almost doable since Jackson has async (non-blocking) parser
implementation too... unfortunately it expects byte-based input. You
could re-encode content as UTF-8 bytes, feed them, so technically this
would be doable.

Other than this approach all individual JSON values are parsed as one
and cannot be streamed.

I hope this helps,

-+ Tatu +-
Reply all
Reply to author
Forward
0 new messages