I have JSON file with below format
{
"key1": ["val1", "val2"],
"key2": ["v1", "v2"]
}
I want to transform it into
{
"key1": ["val1-v1", "val2-v2"],
"key2": ["v1", "v2"]
}
How can I acheive this using jq in linux environment?
>Solution :
You could use to_entries to generate an array of indices and values. Use the indices to index into the other key, stored in a variable for reference:
jq '. as {$key2} | .key1 |= [to_entries[] | .value + "-" + $key2[.key]]' data.json
If the arrays aren’t big, you could also use the less efficient transpose filter to zip them:
jq '.key1 = [map(.) | transpose[] | join("-")]' data.json
Output of both:
{
"key1": [
"val1-v1",
"val2-v2"
],
"key2": [
"v1",
"v2"
]
}