Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to reconstruct array data?

I have this data:

{
  "data": [
    {
      "a": 11,
      "b": 12,
      "c": 13
    },
    {
      "a": 21,
      "b": 22,
      "c": 23
    },
    {
      "a": 31,
      "b": 32,
      "c": 33
    }
  ]
}

I want to collate it into arrays based on the key names like this:

{
  "data": [
    {
      "a": [
        11,
        21,
        31
      ]
    },
    {
      "b": [
        12,
        22,
        32
      ]
    },
    {
      "c": [
        13,
        23,
        33
      ]
    }
  ]
}

For this example we can assume that each element of the original array has the same set of keys, just different values for them.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This jq code can do it for select keys:

jq '(.data[0]|keys_unsorted) as $keys | { ($keys[2]): [.data[]|.[$keys[2]]] }' data.json
{
  "c": [
    13,
    23,
    33
  ]
}

How do I do this for all keys?

>Solution :

One way would be using to_entries to access the keys, then group_by for the grouping, and map to construct the final structure:

.data |= (map(to_entries[]) | group_by(.key) | map({(first.key): map(.value)}))
{
  "data": [
    {
      "a": [
        11,
        21,
        31
      ]
    },
    {
      "b": [
        12,
        22,
        32
      ]
    },
    {
      "c": [
        13,
        23,
        33
      ]
    }
  ]
}

Suggestion: Instead of having an array of objects with a single field, you could instead build one object containing all fields:

.data |= reduce (.[] | to_entries[]) as {$key, $value} ({}; .[$key] += [$value])
{
  "data": {
    "a": [
      11,
      21,
      31
    ],
    "b": [
      12,
      22,
      32
    ],
    "c": [
      13,
      23,
      33
    ]
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading