# Convert Json table arrays to objects with jq

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.

``````jq 'to_entries|map(.key) as \$keys| (map(.value)|transpose) as \$values |\$values|map([\$keys, .] | transpose| map( {(.): .} ) | 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( {(.): .} ) | 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).

``````\$ jq '[.IdentifierName, .Code] | transpose | map( { "IdentifierName": ., "Code": . } ) ' file.json
[
{
"IdentifierName": "A",
"Code": 5
},
{
"IdentifierName": "B",
"Code": 8
},
{
"IdentifierName": "C",
"Code": 19
}
]
``````

I'm extracting the two arrays into an array (of two arrays) that I transpose. For the given data, this produces `[["A",5],["B",8],["C",19]]`. I then map each individual element of this array to an object with the wanted keys.

This could possibly be made neater somehow. For example, it would be nice to not have to reconstruct the objects at the end with explicit key names, and it would also be nice if one could generalize this to n sub-arrays, not just two.

• This is helpful, am I still trying for a generic answer. May 26, 2020 at 19:01
{"ft_lang_label":"__label__en","ft_lang_prob":0.7873339,"math_prob":0.8998647,"size":702,"snap":"2023-40-2023-50","text_gpt3_token_len":200,"char_repetition_ratio":0.13896848,"word_repetition_ratio":0.0,"special_character_ratio":0.32905984,"punctuation_ratio":0.23841059,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9600692,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-12-01T18:51:13Z\",\"WARC-Record-ID\":\"<urn:uuid:b1c28bc8-5fe0-4d35-9be2-dc6fe0f7c30b>\",\"Content-Length\":\"154934\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:ee765ef4-d4a7-4b02-aaf5-cd89880acc3b>\",\"WARC-Concurrent-To\":\"<urn:uuid:305a7ec3-0a70-467a-a0ef-c33f7f618ebd>\",\"WARC-IP-Address\":\"104.18.43.226\",\"WARC-Target-URI\":\"https://unix.stackexchange.com/questions/589099/convert-json-table-arrays-to-objects-with-jq\",\"WARC-Payload-Digest\":\"sha1:6XPXSOVE5XM6Z5WO4WYES44PDJANRKVB\",\"WARC-Block-Digest\":\"sha1:F23UQGDFIYFIVGX6BPPLFFLXCNIHB57X\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679100304.52_warc_CC-MAIN-20231201183432-20231201213432-00104.warc.gz\"}"}