6

I have a package.json file that looks like this:

{
  "name": "service",
  "version": "1.0.0",
  "private": true,
  "description": "my service",
  "license": "none",
  "scripts": {
    "build": "tsc"
  },
  "dependencies": {
    "@mycompany/mypackage": "1.1.1"
  }
}

I want to update the @mycompany/mypackage version to "1.2.1". I found articles like this and this suggesting jq.

When I run my jq command jq -r '"dependencies.@mycompany/mypackage" |= "1.2.1"' package.json, I get this error: jq: error (at temp.json:13): Invalid path expression with result "dependencies.@mycompany/m...

From what I can tell the path is correct when I used jq play.

Does anyone have a suggestion what is wrong?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255

1 Answers1

11

You're pretty close. You do need to quote the @mycompany/mypackage key, because both the @ and / are special characters, but you have to quote it separately, not as part of the whole filter. Also you need a leading . to query the root object. This should work:

$ jq -r '.dependencies."@mycompany/mypackage" |= "1.2.1"' package.json
{
  "name": "service",
  "version": "1.0.0",
  "private": true,
  "description": "my service",
  "license": "none",
  "scripts": {
    "build": "tsc"
  },
  "dependencies": {
    "@mycompany/mypackage": "1.2.1"
  }
}
Michael Mrozek
  • 93,103
  • 40
  • 240
  • 233