Convert Json table arrays to objects with jq

user221826

I am trying to convert a json structure that key1: array of values, key2: array of values ,.... to an array of objects. The size of arrays is same and each object is just aggregate of items at position x in each array. Need help converting this preferably with generic code with jq.

Input

{
  "IdentifierName": [
    "A",
    "B",
    "C"
  ],
  "Code": [
    5,
    8,
    19
  ]
}

Expected Output

[
  {
    "IdentifierName": "A",
    "Code": 5
  },
  {
    "IdentifierName": "B",
    "Code": 8
  },
  {
    "IdentifierName": "C",
    "Code": 19
  }
]

Edit: progress so far:

jq 'to_entries|map(.key) as $keys| (map(.value)|transpose) as $values |$values|map($keys, .)'

The last step is to somehow index with the keys into the values that I am still unable to get right.

user221826

Answering my own question:

jq 'to_entries|map(.key) as $keys| (map(.value)|transpose) as $values |$values|map([$keys, .] | transpose| map( {(.[0]): .[1]} ) | add)'

Explanation: Extract keys ["IdentifierName", "Code"] and values as [ [ "A", 5 ], [ "B", 8 ], [ "C", 19 ] ]
Then to index from keys to values, take json-seq of key-tuple with (each) value tuple and transpose and zip them in pairs.

 echo '[[
    "IdentifierName",
    "Code"
  ],
  [
    "C",
    19
  ]
]'|jq '.|transpose| map( {(.[0]): .[1]} ) | add'

Combining both gives solution. This will work for any number of elements (0 and 1 are just key and value, not first and second).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Convert json single-item arrays to objects using bash/jq

jq - JSON convert keys into objects

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

jq: convert JSON file with arrays to CSV - transposed?

Convert JSON of arrays to CSV with headers using JQ

how to Convert Table to Json Arrays?

Convert json to long format table using jq

How to convert Json table (array of arrays) data to a collection of c# objects

How to merge different objects and arrays in the same json file using jq

Convert JSON array of arrays to map of objects (Java)

How to convert a JSON with arrays in tsv with jq keeping the array structure?

How to convert intermittent string elements in a json to arrays using jq

How to split a string value in json and convert to nested objects using jq?

Modify arrays within objects in jq

How to merge with jq json arrays elements where elements are objects with nested arrays?

How to merge JSON objects with sub arrays with adding objects with matching values inside the array instead of replacing them with jq?

jq: Convert json to text

Convert JSON to CSV with jq

Convert string to json in jq

Using jq and map to convert JSON objects with nested objects to Name=Value format

Flattening JSON lines arrays with JQ

Converting tsv with arrays to JSON with jq

jq: groupby and nested json arrays

Processing JSON of arrays in array with jq

How do I show only top level JSON entries that are not arrays or objects with jq version 1.3?

SQL Server: Transform arrays of JSON objects into table format

jq — Extract strings contained in nested arrays and objects

Delete objects and arrays with jq which match a key

Append JSON Objects using jq