Skip to content

Instantly share code, notes, and snippets.

@lsloan
Forked from joar/jq-insert-var.sh
Last active August 24, 2022 17:37
Show Gist options
  • Save lsloan/c392e0e9c209cf2349956928762592b7 to your computer and use it in GitHub Desktop.
Save lsloan/c392e0e9c209cf2349956928762592b7 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 '. + {foo: ("not" + $foo)}'
# {
# "hello": "world",
# "foo": "notbar"
# }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment