Idan Adar
Idan Adar

Reputation: 44516

How to modify a key's value in a JSON file from command line

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

Answers (4)

Reino
Reino

Reputation: 3423

With :

$ 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

Danila Vershinin
Danila Vershinin

Reputation: 9845

With the sde CLI utility, you can

sde name adar package.json

Upvotes: 1

Pushkar Soni
Pushkar Soni

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

Idan Adar
Idan Adar

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

Related Questions