2021年1月19日星期二

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 }  
https://stackoverflow.com/questions/65800468/flatten-merge-json-into-single-array-of-objects-with-jq January 20, 2021 at 06:30AM

没有评论:

发表评论