Transforming arrays of varying length into objects based on the parity of their indices in JQ

skeleton_mouse

Basically i need to transform a set of arrays depending on if the index is odd or even, while the length of each array is different. What i have been unable to do is to transform and add the additional arrays into the right children array, which im guessing should be done based on if the index of the array is odd or even. I need a universal solution, which would work on a bigger amount of data also.

json example:

[
  [
    [
      "dev",
      5
    ],
    [
      [
        "RT",
        1
      ],
      [
        "CP",
        2
      ]
    ]
  ],
  [
    [
      "jez",
      2
    ],
    [
      [
        "ME",
        1
      ],
      [
        "PW",
        1
      ]
    ],
    [
      "lem",
      3
    ],
    [
      [
        "AR",
        1
      ],
      [
        "ME",
        2
      ]
    ],
    [
      "has",
      1
    ],
    [
      [
        "MOBILE",
        1
      ]
    ]
  ]
]

transform into:

{
  "children": [
    {
      "name": "dev",
      "value": 5,
      "children": [
        {
          "name": "RT",
          "value": 1
        },
        {
          "name": "CP",
          "value": 2
        }
      ]
    }
  ]
}
{
  "children": [
    {
      "name": "jez",
      "value": 2,
      "children": [
        {
          "name": "ME",
          "value": 1
        },
        {
          "name": "PW",
          "value": 1
        }
      ]
    },
    {
      "name": "lem",
      "value": 3,
      "children": [
        {
          "name": "AR",
          "value": 1
        },
        {
          "name": "ME",
          "value": 2
        }
      ]
    },
    {
      "name": "has",
      "value": 1,
      "children": [
        {
          "name": "MOBILE",
          "value": 1
        }
      ]
    }
  ]
}

what i have been able to transform it into(jqplay: https://jqplay.org/s/7nZEbfRTKDz)

{
  "children": [
    {
      "name": "dev",
      "value": 5,
      "children": [
        {
          "name": "RT",
          "value": 1
        },
        {
          "name": "CP",
          "value": 2
        }
      ]
    }
  ]
}
{
  "children": [
    {
      "name": "jez",
      "value": 2,
      "children": [
        {
          "name": "ME",
          "value": 1
        },
        {
          "name": "PW",
          "value": 1
        }
      ]
    }
  ]
}

thanks in advance for any answers

oguz ismail

This should work if your actual data doesn't contain deeper arrays than in your example:

def f: {
  name: .[0],
  value: .[1]
};
{
  children: [
    _nwise(2) | (.[0] | f) + {
      children: (.[1] | map(f))
    }
  ]
}

Online demo

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Transforming nested array of objects using jq

JQ get unique objects and arrays based on key value

Type hints for recursive and varying length objects in python

Modify arrays within objects in jq

PySpark Using collect_list to collect Arrays of Varying Length

Brightness plot (or image) with rows corresponding to arrays of varying length

Reformatting deeply nested JSON object with varying length into an array of simplified objects

How to inject the array indices of objects in an array into the objects, using jq

Transforming array of objects containing arrays into a unique object lookup with lodash

Transforming Pandas pivot-table to multiple arrays of objects

Dynamically format string based on varying number/length of values/tuples

Split pandas string column based on varying length separator

Sorting li in unordered list based on data attribute with varying length (no jquery)

transforming configMapKeyRef into secretKeyRef with jq

Split Json Arrays based on length

jq — Extract strings contained in nested arrays and objects

Delete objects and arrays with jq which match a key

Convert Json table arrays to objects with jq

Explode Complex JSON Object (with Objects and Arrays) with jq

Subset json based on string list of indices using jq

Transforming a nested data frame with varying number of elements

Select objects based on the key in hash using jq

JQ Select Outer Objects and Child Based on Child

jq - create new object based on existing objects

JQ remove objects based on key value

Merging array of objects based on values in JQ

Reading in records of varying length

averaging datasets of varying length

Vector definition with varying length