I have a JSON file with a bunch of objects and after each, there's a smaller object with extra data. They don't share any information, so the position is the only thing connecting them. This unfortunate structure is due to limitations of OverpassQL.
The challenge is how to merge each pair in the input, e.g. in the simpler case ["a","b","c","d"]
to ["ab","cd"]
.
If I put it all in a scripted loop outside of jq and reconstructed the JSON afterwards, something as simple as this would work (but it'd be quite lame):
$ jq '.elements[0] + .elements[1]' test.json
So I generated the indices to then use them in the original data:
$ jq -n 'range(0; 2) | foreach . as $idx (0; $idx; [2 * $idx, 2 * $idx + 1])'[ 0, 1][ 2, 3]
But combining the two still only gives me the first pair. If I use input
instead, it also prints an error on exit. A simplified version:
$ jq -n 'range(0; 2) | foreach . as $idx (0; $idx; inputs | nth(2 * $idx; .[]) + nth(2 * $idx + 1; .[]))'<<< '["a","b","c","d"]'"ab"
I tried a few other things, but to no avail. Is there a way to merge all consecutive objects in just jq
?