A JSON value other than an array will just be extracted un-modfied, except we’ll stick a newline on the end, and remove any newlines from within the record.
The main use case of this tool is to unpack arrays, but as NDJSON can
be any type we don’t raise an error if this is called with something
other than array. However, you can apply the -expect-array
flag to
have json2nd
check that whatever we’re processing is an array.
echo "{}" | json2nd -expect-array > object.json # will error
Say you have a stream of JSON sperated by whitespace, probably newlines (sounds like NDJSON eh?) but your objects may have been pretty printed so won’t actually work as NDJSON.
printf "{\n}\n{} {\n} 1234" | json2nd # output will be valid NDJSON
One caveat at this time is that if you hit an array json2nd will
error, because it’s default behaviour with respect to arrays conflicts
with what’s expected here. If you are expecting a JSON stream that may
contain arrays you probably want to use the -preserve-array
switch.
Although not it’s true purpose:
json2nd -path thing.i.want file.json
is quite easy to remember when you want to grab any old thing from some json.