Philippe
Philippe

Reputation: 26547

Change value for all keys with jq

I have this package.json

{
  "dependencies": {
    "body-parser": "^1.19.0",
    "eslint": "^7.15.0",
    "express": "^4.17.1"
  } 
} 

with this command :

jq '.dependencies.eslint="latest"|.dependencies.express="latest"' package.json

I got this result :

{
  "dependencies": {
    "body-parser": "^1.19.0",
    "eslint": "latest",
    "express": "latest"
  }
}

How can I change all versions to "latest" without enumerating individual keys ?

Upvotes: 1

Views: 729

Answers (3)

Dmitry
Dmitry

Reputation: 1293

if package.json by chance contains nested items, like this:

{
  "dependencies": {
    "body-parser": "^1.19.0",
    "eslint": "^7.15.0",
    "express": "^4.17.1",
    "branch": {
        "alternative": "^1.2.3"
    }
  } 
}

then an alternative (non-jq) solution, using jtc would look like this:

<package.json jtc -w'<>a:' -u'"latest"'

- that solution won't break the original JSON structure.

PS. I'm the developer of jtc unix JSON processor.
PPS. the disclaimer is required by SO.

Upvotes: 2

peak
peak

Reputation: 116740

A succinct, precise, and easy-to-read solution:

.dependencies |= map_values("latest")

Upvotes: 3

hek2mgl
hek2mgl

Reputation: 157992

Like this:

jq '.dependencies[]="latest"' package.json

Output:

{
  "dependencies": {
    "body-parser": "latest",
    "eslint": "latest",
    "express": "latest"
  }
}

Upvotes: 5

Related Questions