Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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"
# }
@dhackney

This comment has been minimized.

Copy link

@dhackney dhackney commented Sep 9, 2017

The third example, "# Concat and add" , does not produce the output shown in the example:

{

"hello": "world",

"foo": "notbar"

}

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

@arno01

This comment has been minimized.

Copy link

@arno01 arno01 commented Jul 3, 2018

This does:

$ echo '{"hello": "world"}' | jq --arg foo bar '. + {foo: ("not" + $foo)}'
{
  "hello": "world",
  "foo": "notbar"
}
@davidmaxwaterman

This comment has been minimized.

Copy link

@davidmaxwaterman davidmaxwaterman commented Jul 30, 2019

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:

$ echo '{ "text": [ "a": "b" ], "tours": [ "id": 42 ] }' | jq '...'
{
  "text": [
    "a": "b"
  ],
  "tours": [
    "id": 42,
    "tour_id": 42
  ]
}

So far, I've come up with:

  jq '.tours[] | .tour_id = .id'

which duplicates the tours id property to tour_id, but I lose all the top level properties, not to mention the output is no longer JSON.

Any ideas?

@chrisharm

This comment has been minimized.

Copy link

@chrisharm chrisharm commented Aug 8, 2019

Try using * instead of +

$ echo '{"hello": {"value": "world"}}' | jq --arg foo bar '. * {"hello": {foo: ("not" + $foo)}}'
{
  "hello": {
    "value": "world",
    "foo": "notbar"
  }
}

@davidmaxwaterman

This comment has been minimized.

Copy link

@davidmaxwaterman davidmaxwaterman commented Aug 9, 2019

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.

@kylebarron

This comment has been minimized.

Copy link

@kylebarron kylebarron commented Feb 21, 2020

To add a key to a nested object, you can use .key +=:

echo '{"hello": {"value": "world"}}' | jq '.hello += {other_key: "new_val"}'
{
  "hello": {
    "value": "world",
    "other_key": "new_val"
  }
}
@nickboldt

This comment has been minimized.

Copy link

@nickboldt nickboldt commented May 1, 2020

Thanks for this! used it to quickly enable experimental features in docker, so I can start playing with buildx inside a VM:

# enable client experimental features like buildx (multiarch)
cat ~/.docker/config.json | jq '. + {experimental: "enabled"}' > ~/.docker/config.json2; mv -f ~/.docker/config.json{2,}
docker buildx 

Ref: https://docs.docker.com/buildx/working-with-buildx/#build-multi-platform-images

@fogfish

This comment has been minimized.

Copy link

@fogfish fogfish commented Jul 1, 2020

A few more examples to insert json:

echo '{"hello": "world"}' | jq --argjson json '{"foo": "bar"}' '. + {foo: $json}'

{
  "hello": "world",
  "foo": {
    "foo": "bar"
  }
}

and same but with files

cat template.json | jq --argjson json "`<snippet.json`" '. + {foo: $json}'
@zrhoffman

This comment has been minimized.

Copy link

@zrhoffman zrhoffman commented Jul 23, 2020

To add a key to a nested object, you can use .key +=:

echo '{"hello": {"value": "world"}}' | jq '.hello += {other_key: "new_val"}'
{
  "hello": {
    "value": "world",
    "other_key": "new_val"
  }
}

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"'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment