Reputation: 44516
Is it possible to change the value
of a key
in a JSON file from command line?
e.g., in package.json:
Change
{
...
...
"something": "something",
"name": "idan"
...
}
To
{
...
...
"something": "something",
"name": "adar"
...
}
Upvotes: 35
Views: 48419
Reputation: 3423
With xidel:
$ xidel -s --in-place package.json -e '($json).name:="adar"'
$ xidel -s --in-place package.json -e 'map:put($json,"name","adar")'
$ xidel -s --in-place package.json -e 'map:merge(($json,{"name":"adar"}),{"duplicates":"use-last"})'
Upvotes: 2
Reputation: 9845
With the sde
CLI utility, you can
sde name adar package.json
Upvotes: 1
Reputation: 9
The anoter way is to open the file itself in terminal :
pico filename.json
edit it then save then exit.
check if correct changes were made:
cat filename.json
Upvotes: -8
Reputation: 44516
One way to achieve it is by using the "json" npm package, e.g.:
json -I -f package.json -e "this.name='adar'"
Another way is by using the jq CLI, e.g.:
mv package.json temp.json
jq -r '.name |= "adar"' temp.json > package.json
rm temp.json
Upvotes: 44