Skip to content

Instantly share code, notes, and snippets.

@Happyholic1203
Forked from joar/jq-insert-var.sh
Created September 14, 2016 12:30
Show Gist options
  • Save Happyholic1203/7916aae5ff380748318209c4774ad648 to your computer and use it in GitHub Desktop.
Save Happyholic1203/7916aae5ff380748318209c4774ad648 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