News for hackers. Stuff that matters.
Re:Tips on adding JSON output to your CLI app by nyuszika7h on Sunday December 05, 2021 @07:00PM (#29454610)
I don't really understand the point about flattening.

> This way I can easily filter the data in jq or other tools without having to traverse levels.

How is doing `jq '.cpu.speed'` any harder than doing `jq '.cpu_speed'`?

IMO as long as you aren't going insane with nesting levels, it's actually better to have a proper structure than dumping everything into an ugly flat object.

Re:Tips on adding JSON output to your CLI app by kbrazil on Sunday December 05, 2021 @07:15PM (#29454709)
The article is not advocating flattening willy nilly. The point is to have bias for flatter structures to make it easier for the user, but of course not all structures can or should be flat. On the other hand, don’t over-engineer your data structure so it makes finding things difficult.

Grabbing an attribute is not necessarily any harder in a deeply nested structure, but filtering based on multiple deeply nested attributes in different branches can make a query quite complex.

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.
Re:Tips on adding JSON output to your CLI app by anyfactor on Monday December 06, 2021 @10:11AM (#29460625)
Deep nesting makes it harder for accessing data through a systematic way.

If you are accessing a deep nested data that means you have to account for layers of existence of keys. If cpu exist then see if speed exist then access speed. Nothing wrong with deep nesting as long as you can guarantee a key and data will be generated but more often than not when the data is not being generated the JSON data and the key will not simply exist.

And people do get carried away with nesting. Also it is nice to have core information available at the surface level of JSON file.