News for hackers. Stuff that matters.
Re:Tips on adding JSON output to your CLI app by nyuszika7h on Sunday December 05, 2021 @07:20PM (#29454745)
> The article is not advocating flattening willy nilly.

It certainly seems like it does when the first example for flattening is oversimplifying an already simple structure that doesn't really need flattening. Maybe that was not meant to be a serious example but rather just for ease of understanding, but then the article should have probably said so.

> filtering based on multiple deeply nested attributes in different branches can make a query quite complex

Can you elaborate on this please? Maybe I'm just too tired to think clearly at 1 am, but I don't see how filtering is any harder. You would just do something like `jq '.foo | select(.bar.baz >= 42 and .qux.moo.asd == "abc")`.

Re:Tips on adding JSON output to your CLI app by kbrazil on Sunday December 05, 2021 @09:30PM (#29455548)
I can’t think of one off the top of my head, so maybe I’m misremembering why I originally decided I preferred flatter structures. It’s possible it was due to some limitation on my part as I was first learning to work with JSON.

Looking back at some other JSON output, like `ip addr`, I’m not seeing anything egregious. There are just a couple useless top-level keys in the `iostat` output that make the queries longer and don’t seem to add value.

Using the flat structure with a type field lends itself well to lazily outputting JSON lines, so maybe that’s why I tend to represent cli output that way. It’s sort of how you would format the data when sending to ELk or Splunk.

Since I initially thought of JC as a cli tool, I was trying to reduce line length. Even the name JC was selected so it wouldn’t significantly increase the line length. So not nesting data inside meta objects is something I would encourage.

Re:Tips on adding JSON output to your CLI app by Supermancho on Sunday December 05, 2021 @10:53PM (#29455936)
> It certainly seems like it does when the first example for flattening is oversimplifying an already simple structure that doesn't really need flattening.

For the purposes of making an example of how the concept is applied, making a verbose example is counterproductive. This doesn't require explanation because developers don't make work for themselves they don't need to, in practice.

Re:Tips on adding JSON output to your CLI app by preseinger on Monday December 06, 2021 @12:03AM (#29456353)
That jq expression is surely more difficult to summon than a line-based grep pattern.
Re:Tips on adding JSON output to your CLI app by kbrazil on Monday December 06, 2021 @01:19AM (#29456773)
Grep would not be able to do numeric comparison, would it?
Re:Tips on adding JSON output to your CLI app by preseinger on Wednesday December 08, 2021 @03:44PM (#29490241)
Not directly, but once you want to do that, IMO, you've moved out of "shell script" territory and into "actual program" territory.