Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
5.5k views
in Technique[技术] by (71.8m points)

Flatten/merge JSON into single array of objects with JQ

I am trying to merge a nested array of objects within a parent json object into a single array of objects with jq. Basically I want to merge each value array of objects into a single values array underneath the data array.

Example Input:

{
  "data": [
    {
      "id": 1,
      "error": "error1",
      "key": "key1",
      "value": [
        {
          "class_name": "namespace_read",
          "in_max": 148,
          "in_min": 112,
          "in_rate": 359750.71875
        },
        {
          "class_name": "namespace_write",
          "in_max": 184,
          "in_min": 152,
          "in_rate": 656.1185913085938
        },
        {
          "class_name": "namespace_test",
          "in_max": 152,
          "in_min": 152,
          "in_rate": 29.93098068237305
        }
      ]
    },
    {
      "id": 2,
      "error": "error2",
      "key": "key2",
      "value": [
        {
          "class_name": "namespace_read",
          "in_max": 156,
          "in_min": 112,
          "in_rate": 459885.03125
        },
        {
          "class_name": "namespace_write",
          "in_max": 176,
          "in_min": 152,
          "in_rate": 8970.888671875
        },
        {
          "class_name": "namespace_test",
          "in_max": 152,
          "in_min": 152,
          "in_rate": 262.3605346679688
        }
      ]
    }
  ]
}

Desired Output:

{
  "data": [
    {
      "values": [
        {
          "id": 1,
          "error": "error1",
          "key": "key1",
          "class_name": "namespace_read",
          "in_max": 148,
          "in_min": 112,
          "in_rate": 359750.71875
        },
        {
          "id": 1,
          "error": "error1",
          "key": "key1",
          "class_name": "namespace_write",
          "in_max": 184,
          "in_min": 152,
          "in_rate": 656.1185913085938
        },
        {
          "id": 1,
          "error": "error1",
          "key": "key1",
          "class_name": "namespace_test",
          "in_max": 152,
          "in_min": 152,
          "in_rate": 29.93098068237305
        },
        {
          "id": 2,
          "error": "error2",
          "key": "key2",
          "class_name": "namespace_read",
          "in_max": 156,
          "in_min": 112,
          "in_rate": 459885.03125
        },
        {
          "id": 2,
          "error": "error2",
          "key": "key2",
          "class_name": "namespace_write",
          "in_max": 176,
          "in_min": 152,
          "in_rate": 8970.888671875
        },
        {
          "id": 2,
          "error": "error2",
          "key": "key2",
          "class_name": "namespace_test",
          "in_max": 152,
          "in_min": 152,
          "in_rate": 262.3605346679688
        }
      ]
    }
  ]
}

I was trying something like below with jq, but am trying to find a more scalable way to craft the output:

{ "id": .data[].id, "error": .data[].error, "key": .data[].key, "className": .data[].value[].class_name, "inMax": .data[].value[].in_max }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Flattening can be done via the idiomatic expression del(A) + A[] here, and what's left is construction of the surrounding structure, which is trivial.

.data |= [{values: map(del(.value) + .value[])}]

Online demo


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...