by simonw · 2y ago · 0 comments
It's basically a line-based format that can represent all of the JSON types and includes support for nested data structures where necessary. What's not to like?
A simple line-based shell variable name=value format works unreasonably well. For example:
# ls --shell-var ./thefile
dir="/home/user" file="thefile" size=1234 ...
# eval $(ls --shell-var ./thefile); echo $size
1234
If this had been in shells and cmdline tools since the beginning it would have saved so much work, and the security problems could have been dealt with by an eval that only set variables, adding a prefix/scope to variables, and so on.Unfortunately it's too late for this and today you'll be using a pipeline to make the json output shell friendly or use some substring hacks that probably work most of the time.
The question was "What's not to like [about JSON output from cmdline tools]?" and the answer is that it's cumbersome to read in a shell and all but requires another pipeline stage.
I didn't even recommend shell variable output and made it clear this isn't today a reasonable solution so I'm not sure where this hostility in the replies comes from, but I assume from recognition that it's a more practical solution to reading data within a shell but not wanting that to be so.
The nature of being nested, and also containing structures like lists, maps, etc. All of which makes it more complicated than key=value.
> The question was "What's not to like [about JSON output from cmdline tools]?" and the answer is that it's cumbersome to read in a shell and all but requires another pipeline stage.
It depends on the intended use for your shell program. If you intend the CLI tool to be used in CI pipelines (eg. your CLI tool's output is being read by an automated process on a computer) and the data it outputs is more complicated than a simple key=value, JSON is great for that. Your CI program can pipe to jq. You as a human can pipe to jq, though I agree it's somewhat less desirable. Though just piping to jq without any arguments pretty prints it for you which also makes it fairly readable for humans.
> so I'm not sure where this hostility in the replies comes from
You're reading into hostility where there isn't any.
It wasn't my request and OP (not me) said "line-based data" is best. The comment I replied to said "Newline-delimited JSON ... a line-based format".
If the only objection you have is "but that's line-based!" then you're in a completely different conversation.
> if anything, json output | jq is more the unix way than piping everything through shell vars.
The unix way is line-based. The comment I replied to is talking about line-based output. Line-based output is the only structure for data universal to unix cmdline tools - even tab/space isn't universal; sending structured non-line-delimited data to a program to unpack it is the least unix-like way to do it.
Also there's no pipe in the shell-variable output scheme I described, whereas "json | jq" is a shell pipeline.
On a separate point, I find the JSON much easier to reason about. The wall of text output doesn’t work for my brain - I just can’t see it all. Structuring/nesting with clear delineations makes it far easier for me to grok.
> Multithreaded processing of gigantic Newline-Delimited JSON (ndjson) and related formats at 3.5 GB/s
[1] https://github.com/simdjson/simdjson?tab=readme-ov-file#bind...