6

I have the following JSON file located at /tmp/target.json:

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    ...
  }
}

I want to add a new key value pair so it will be as follows:

{
  "compileOnSave": false,
  "compilerOptions": {
    "skipLibCheck": true,
    "baseUrl": "./",
    ...
  }
}

I use the following command but it doesn't work:

jq --argjson addobj '{"skipLibCheck": "true"}' '
  .compilerOptions{} |= $addobj
' /tmp/target.json

I gives me this error:

jq: error: syntax error, unexpected '{', expecting $end (Unix shell quoting issues?) at <top-level>, line 2:
  .compilerOptions{} |= $addobj                  
jq: 1 compile error

What have I done wrong? How can I get it to work as intended?

2 Answers 2

8

Like this:

$ jq '.compilerOptions.skipLibCheck=true' file.json
{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "skipLibCheck": true
  }
}
0
4

The easiest way to add the key with its value has been covered in another answer. That answer adds the key to the end of the list of keys in the compilerOptions object. Normally, the ordering of the keys does not matter, and if you need things ordered in a particular way, you will use an array. However, I'm noticing that you (for whatever reason) expect the key to be added first, before the existing baseUrl key.

We can add the key in that position by, instead of adding the new key to the existing object, instead, add the existing object's keys to the end of the new key. So given the existing JSON document,

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "jerry": "Was a race car driver"
  }
}

... we may want to use

jq --argjson skipLibCheck true '.compilerOptions = $ARGS.named + .compilerOptions' file

Given our example document above, this would generate

{
  "compileOnSave": false,
  "compilerOptions": {
    "skipLibCheck": true,
    "baseUrl": "./",
    "jerry": "Was a race car driver"
  }
}

The $ARGS.named thing is an object which contains the key-value pairs defined with --arg and/or --argjson on the command line. In the example above, this would be {"skipLibCheck":true}. Note that the $ARGS feature was introduced after release 1.5 of jq.

With the older 1.5 release of jq, you may use

jq --argjson skipLibCheck true '.compilerOptions = { skipLibCheck: $skipLibCheck } + .compilerOptions' file

Use --arg instead of --argjson if you want the value to be the string true rather than the special boolean value true.

The following gives an alternative way of adding the key at the end (to what's mentioned in the other answer), which follows the same pattern as the above command. Note that I'm also switching to using --arg here to insert true as a string, just to show how that looks.

jq --arg skipLibCheck true '.compilerOptions += $ARGS.named' file

... which would give you

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "jerry": "Was a race car driver",
    "skipLibCheck": "true"
  }
}

With the older 1.5 release of jq, you may use

jq --arg skipLibCheck true '.compilerOptions += { skipLibCheck: $skipLibCheck }' file
4
  • @CaTx I saw your comment about $ARGS not being recognised in jq release 1.5. Note that release 1.6 has been available since the beginning of November 2018 and that this newer version, as well as supporting $ARGS, has several other enhancements and fixes. If at all possible, you should update to a newer release of jq. The tool is available as a static binary for Linux at stedolan.github.io/jq
    – Kusalananda
    Commented Feb 17, 2023 at 6:51
  • Thank you so much, @Kusalananda! After switching to jq 1.6, your code works. What should the syntax be it it were jq 1.5?
    – CaTx
    Commented Feb 17, 2023 at 7:20
  • @CaTx See updated answer.
    – Kusalananda
    Commented Feb 17, 2023 at 7:31
  • Thank you! jq 1.6 surely is much more intuitive. They should use this version for the package repo.
    – CaTx
    Commented Feb 17, 2023 at 7:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .