I have the following JSON structure:
[
{
"key11": "value11",
"key12": "value12",
"key13": "value13",
"key14": "value14"
},
{
"key21": "value21",
"key22": "value22",
"key23": "value23",
"key24": "value24"
},
{
"key31": "value31",
"key32": "value32",
"key33": "value33",
"key34": "value34"
}
]
How do I convert it to the following structure:
{
"value11": "value12",
"value21": "value22",
"value31": "value32"
}
I have tried a few JQ operators, but nothing seems to work.
3
Answers
Yes, you could, for example, read the string keys as arguments, and resolve them in an iterative (or mapping) context.
One way would be to
reduce
the input into an initially empty output object{}
, using the key ofto_entries
to access the iteration index, which in turn can be used to index into the argument list:With this effort, however, you could also just map the input array, and use
from_entries
to generate the output object, enabling you to use thewith_entries
shortcut:So, assuming your input with all objects having the same keys looks like
Then, you could either use
map
to turn each array item to a single-field object, and thenadd
to combine these objects into one (Demo):Or you could
reduce
the array’s items into an initially empty output object{}
by iteratively setting another of its keys (Demo):Both approaches return: