Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"      ... } 
like image 603
Idan Adar Avatar asked Apr 08 '17 09:04

Idan Adar


1 Answers

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 
like image 100
Idan Adar Avatar answered Sep 21 '22 04:09

Idan Adar