Skip to content

Instantly share code, notes, and snippets.

@VimleshS
Forked from joar/jq-insert-var.sh
Created January 20, 2022 08:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VimleshS/9e42e009cd3aa48b6723a80a8f8e294f to your computer and use it in GitHub Desktop.
Save VimleshS/9e42e009cd3aa48b6723a80a8f8e294f to your computer and use it in GitHub Desktop.
Add a field to an object with JQ
# 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"
# }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment