0

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 }

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.