unix命令格式化扁平化嵌套json对象数据

unix命令格式化扁平化嵌套json对象数据

输入数据格式 json 示例

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

预期结果应格式化为 flatten json

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

字段可能是未知的!因此无需激活任何过滤命令!

基于 UNIX 的命令有什么想法吗?

答案1

看一下格罗恩。从链接页面:

使 JSON 可 grep 化!

gron将 JSON 转换为离散分配,以便更轻松地 grep 查找您想要的内容并查看它的绝对“路径”。它简化了对返回大量 JSON 但文档很糟糕的 API 的探索。

▶ 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 = "[email protected]";
json[0].commit.author.name = "Tom Hudson";

gron 也可以向后工作,使您能够将过滤后的数据转回 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": "[email protected]",
        "name": "Tom Hudson"
      }
    }
  }
]

答案2

Jq是处理 JSON 数据的正确工具(https://stedolan.github.io/jq/manual/v1.5/)。

样本input.json

{
  "data": {
    "div1": {
      "name": "some name",
      "age": 1,
      "address_1": "some address",
      "items": {
        "item_x1": "some data"
      }
    },
    "div2": {
      "name": "some other name",
      "age": 2,
      "address_2": "some address",
      "items": {
        "item_x2": "some data"
      }
    },
    "div3": {
      "name": "another name",
      "age": 3,
      "address_3": "some address",
      "items": {
        "item_x3": "some data"
      }
    }
  }
}

jq -c '"data" as $main_k | .data as $data | .data | to_entries
       | group_by(.key) | map(from_entries)[] | [paths(scalars)]
       | map(("\($main_k)." + join(".")) as $key
             | {($key): (reduce .[] as $k ($data; . = .[$k]))})
       | add' input.json

输出:

{"data.div1.name":"some name","data.div1.age":1,"data.div1.address_1":"some address","data.div1.items.item_x1":"some data"}
{"data.div2.name":"some other name","data.div2.age":2,"data.div2.address_2":"some address","data.div2.items.item_x2":"some data"}
{"data.div3.name":"another name","data.div3.age":3,"data.div3.address_3":"some address","data.div3.items.item_x3":"some data"}

相关内容