@niklas.collin I believe cheshire can do this lazily
Yeah could be. I ended up actually writing JSONL format which suits better in my use case
And for that jsonista works just fine since I write a new JSON object per line
There's also a command line tool which supports streaming: https://github.com/borkdude/jet
nice. Doesn't support Windows though, at least the installer doesn't 😜
is JSONL just writing json objects, one on each line?
And apparently it does graalvm native compilation
Yeah, it basically is
It's like "better CSV"
that's right, no pre-built Windows, but if you build one on your own machine it may work in Windows. Also you can maybe use it from the JVM, but in that case, I would just look at how streaming was implemented and copy it. But probably jsonista already supports it like that (passing a reader, read objects until you hit EOF).
yeah
cool
and actually on windows powershell can do that stuff out of the box excluding edn/transit stuff of course
that's nice. there may also be a jq for Windows
I guess there is but can't see why I would use one 🤷
when native tools work just fine
right. can you show me an example of how you use it in Powershell? PS also runs on Linux and Mac nowadays
well, it of course totally depends on what one is doing...
but a random example:
(irm <http://foo.bar/some.json>).somePathInJsonObject.andAnother | select key1,key2
would handle json which would be of format:
{"somePathInJsonObject": {"andAnother": [{"key1": 1, "key2": 2, "key3": 3}, {"key1": 4, "key2": 5, "key3": 6}]}}
and naturally objects could have more keys and in this example it would retrieve the values in the array but only keys key1 and key2
and if one passes the result into Format-Table
then it gets rendered as a nice table
but that is not very usable anymore since then data isn't objects anymore
then one would have to go to the *nix way of string manipulation 🙂
Ah. Just for fun I tried this with jet:
$ jet --from json --keywordize --query '[:somePathInJsonObject :andAnother (map (select-keys [:key1 :key2]))]' <<< '{"somePathInJsonObject": {"andAnother": [{"key1": 1, "key2": 2, "key3": 3}, {"key1": 4, "key2": 5, "key3": 6}]}}'
[{:key1 1, :key2 2} {:key1 4, :key2 5}]
thanks for showing the example
but tl;dr: lazily consuming elements of a top-level array is something cheshire supports, jsonista, don't know
jsonista is 1:1 wrapper for Jackson Databind, which is not by default, streaming. Databind builds on top of Jackson Streaming could be exposed in jsonista. PRs most welcome.
yeah, I guess the most common use case is top level objects streaming which you now can already do with an inputstream or reader
JsonFactory f = mapper.getFactory(); // may alternatively construct directly too
// First: write simple JSON output
File jsonFile = new File("test.json");
JsonGenerator g = f.createGenerator(jsonFile);
// write JSON: { "message" : "Hello world!" }
g.writeStartObject();
g.writeStringField("message", "Hello world!");
g.writeEndObject();
g.close();
// Second: read file back
JsonParser p = f.createParser(jsonFile);
JsonToken t = p.nextToken(); // Should be JsonToken.START_OBJECT
t = p.nextToken(); // JsonToken.FIELD_NAME
if ((t != JsonToken.FIELD_NAME) || !"message".equals(p.getCurrentName())) {
// handle error
}
t = p.nextToken();
if (t != JsonToken.VALUE_STRING) {
// similarly
}
String msg = p.getText();
System.out.printf("My message to you is: %s!\n", msg);
p.close();
yes, true that. Does that apply also to writing a lazy sequence into outputstream?