Supposing that I have a JSON file that looks like this:
{
"name": "tom",
"scripts": {
"webpack": "webpack --progress --colors --watch"
},
"dependencies": {
},
"devDependencies": {
"webpack": "^2.2.1"
}
}
I would like to be able to specify a package by name, find the corresponding entry in either dependencies or devDependencies and update the value.
The closest I got was this:
$ jq --arg p webpack --arg v 1.2.3 'to_entries | map(
if (.value[$p]? | startswith("^")?) then
.value[$p] = $v
else .
end
) | from_entries' file.json
Which updates the value but also removes the dependencies and the name property:
{
"scripts": {
"webpack": "webpack --progress --colors --watch"
},
"devDependencies": {
"webpack": "1.2.3"
}
}
How can I update the desired value without affecting the other properties in the original JSON?