Unix command formatting flattening nested json objects data

command linejsontext formatting

example input data format json

data: {
   div1: {
      name: "some name",
      age: number,
      address_1: "some address",
      items: {
         item_x1: "some data",
         ..
         ..
      }
   }
   ..
   ..
}

expected result should be formatted flatten json

{ "data.div1.name":"some name",..., "data.div1.items.item_x1":"some data",...},
..
..
{ "data.divN.name":"some name",... }

fields can be unknown! so there is not necessary to activate any filtration command!

any ideas for unix based command ?

Best Answer

Have a look at gron. From the linked page:

Make JSON greppable!

gron transforms JSON into discrete assignments to make it easier to grep for what you want and see the absolute 'path' to it. It eases the exploration of APIs that return large blobs of JSON but have terrible documentation.

▶ gron "https://api.github.com/repos/tomnomnom/gron/commits?per_page=1" | fgrep "commit.author"
json[0].commit.author = {};
json[0].commit.author.date = "2016-07-02T10:51:21Z";
json[0].commit.author.email = "mail@tomnomnom.com";
json[0].commit.author.name = "Tom Hudson";

gron can work backwards too, enabling you to turn your filtered data back into JSON:

▶ gron "https://api.github.com/repos/tomnomnom/gron/commits?per_page=1" | fgrep "commit.author" | gron --ungron
[
  {
    "commit": {
      "author": {
        "date": "2016-07-02T10:51:21Z",
        "email": "mail@tomnomnom.com",
        "name": "Tom Hudson"
      }
    }
  }
]
Related Question