'How to append a json variable to another?
I want to avoid using JSON files and use variables but it doesn't work
element='{"x": "zero"}'
example='{
"a": "one",
"b": "two",
"c": "three",
}'
jq -s '.[0] * .[1]' element.json example.json
If the contents of the JSON files are exactly as what's inside the single quotes of the vars it works as expected. But I also want the element to be the first one (with * it doesn't go first, with +=, it makes another object)
If I replace the filenames with $element and $example, it fails (expecting a file)
I tried like this:
jq --argjson el "$element" --argjson ex "$example" '$el += $ex'
but it says jq: invalid JSON text passed to --argjson. I don't get it. The vars seem to be correct JSON.
I just want this output, "x" first, using variables not files:
{
"x": "zero",
"a": "one",
"b": "two",
"c": "three",
}
Solution 1:[1]
It worked using the comment suggestions like this
element='{"x":"zero"}'
example='{
"a":"one",
"b":"two",
"c":"three"
}'
jq -n --argjson el "$element" --argjson ex "$example" '$el + $ex'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
