# Add field | |
echo '{"hello": "world"}' | jq --arg foo bar '. + {foo: $foo}' | |
# { | |
# "hello": "world", | |
# "foo": "bar" | |
# } | |
# Override field value | |
echo '{"hello": "world"}' | jq --arg foo bar '. + {hello: $foo}' | |
{ | |
"hello": "bar" | |
} | |
# { | |
# "hello": "bar" | |
# } | |
# Concat and add | |
echo '{"hello": "world"}' | jq --arg foo bar '. + {hello: ("not" + $foo)}' | |
# { | |
# "hello": "world", | |
# "foo": "notbar" | |
# } |
This comment has been minimized.
This comment has been minimized.
This does:
|
This comment has been minimized.
This comment has been minimized.
I have json which is an object containing a property that is an array, and I want to create another property with the same value as an existing property, without changing anything else. eg:
So far, I've come up with:
which duplicates the Any ideas? |
This comment has been minimized.
This comment has been minimized.
Try using
|
This comment has been minimized.
This comment has been minimized.
That's great, thanks :) FWIW, I think I ended up using a cli tool called 'underscore', which allowed me to run plain old js code. |
This comment has been minimized.
This comment has been minimized.
To add a key to a nested object, you can use
|
This comment has been minimized.
This comment has been minimized.
Thanks for this! used it to quickly enable experimental features in docker, so I can start playing with buildx inside a VM:
Ref: https://docs.docker.com/buildx/working-with-buildx/#build-multi-platform-images |
This comment has been minimized.
This comment has been minimized.
A few more examples to insert json:
and same but with files
|
This comment has been minimized.
This comment has been minimized.
If you are only adding or modifying a single value, there is no need to merge JSON. Same output as above: echo '{"hello": {"value": "world"}}' | jq '.hello.other_key = "new_val"' |
This comment has been minimized.
The third example, "# Concat and add" , does not produce the output shown in the example:
This is the actual output:
$
echo '{"hello": "world"}' | jq --arg foo bar '. + {hello: ("not" + $foo)}'
{
"hello": "notbar"
}
tested on:
jq --version: jq-1.5-1-a5b5cbe
and
jq --version: jq-1.5