0

Title says it all. So far I get this:

echo '[{"a": "A", "b": "B"}, {"c": "C", "d": "D"}]' | jq '.[] + {x:"X"}'

Which will result to this:

{
  "a": "A",
  "b": "B",
  "x": "X"
}
{
  "c": "C",
  "d": "D",
  "x": "X"
}

but it will not "wrap" the result in array and also does not add comma after each entry.

1 Answer 1

2
jq '[.[] + {x:"X"}]'

Or you could use map

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you. Can you plase also add map version just for clarity?
@WakanTanka: You can just do map(.+ {x:"X"})
thank you both. I've found that this also works jq '.[] += {x:"X"}' Can you please briefly explain why my original approach is not working? Because if I use jq '.' then the output is correct JSON but when I use jq '[.]' then the output is same JSON but nested in one more array.
That's a good point about using += here. To understand all this, I'd suggest reading the relevant parts of the online jq manual while using the debug filter so you can see exactly what's going on at each step.
@peak can you please post how can I use debug in those 3 cases? I'm trying to run following but I got errors: jq 'debug [.[] + {x:"X"}]'
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.