Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent escaping when adding JSON object with jq?

Tags:

json

bash

jq

I am working with jq and I am trying to add a new JSON object to a new key to an existing file.

I have the following JSON file, foobarbaz.json :

{
    "example":{
        "name": "stackOverflowQuestion"
    }
}

I want to add a new entry under example, so to get the following output in foobar.json

{
    "example": {
        "name": "stackOverflowQuestion",
        "new": {
            "newfield": {
                "key": "value"
            }
        }
    }
}

I am using the following commands in the terminal:

$ tempvar='{"newfield":{"key":"value"}}'
$ cat foobarbaz.json | jq '.example.new=env.tempvar' > foobar.json

However, the output in foobar.json is somewhat unexpected:

{
  "example": {
    "name": "stackOverflowQuestion",
    "new": "{\"newfield\":{\"key\":\"value\"}}"
  }
}

Why does jq wrap the curly brackets with quotes, and why does it escape the double quotes?

like image 797
Paolo Avatar asked Dec 22 '25 00:12

Paolo


2 Answers

Use the --argjson option to pass the pre-existing JSON snippet as a variable to the filter.

$ jq --argjson x "$tempvar" '.example.new=$x' foobarbaz.json
{
  "example": {
    "name": "stackOverflowQuestion",
    "new": {
      "newfield": {
        "key": "value"
      }
    }
  }
}

Note that tempvar isn't strictly necessary and can be dropped, if you are only defining it for use with the filter:

$ jq '.example.new={newfield: {key: "value"}}' foobarbaz.json
{
  "example": {
    "name": "stackOverflowQuestion",
    "new": {
      "newfield": {
        "key": "value"
      }
    }
  }
}
like image 167
chepner Avatar answered Dec 25 '25 22:12

chepner


Use fromjson to convert your string (the format all environment variables are in!) to the corresponding data structure, by decoding it as JSON content.

tempvar='{"newfield":{"key":"value"}}' jq '.example.new=(env.tempvar | fromjson)' <<'EOF'
{
    "example":{
        "name": "stackOverflowQuestion"
    }
}
EOF

...emits as output:

{
  "example": {
    "name": "stackOverflowQuestion",
    "new": {
      "newfield": {
        "key": "value"
      }
    }
  }
}
like image 38
Charles Duffy Avatar answered Dec 25 '25 22:12

Charles Duffy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!