change json file by bash script -
I need your help to solve the following problem: I have a JSON file that looks like this: {"key1": "value1", "key2": "value2", "key3": "value3"}
How do I create a new key Can I add "code4" to "code4": "value4" ) by Bash script?
Thank you
sudo apt-get jq < Li> install on OSX, with a homebree, install Jq For example, depending on the following input string - output < Code> stdout :
jsonStr = "{" Key1 ":" value1 "," key2 ":" value2 "," key3 ":" value3 "} ' Remove "key3": jq 'del (.key3)' & lt; & lt; "$ jsonStr" value with value "value4" Add the key "key4": jq '"{1 key4": "value4"}' << code & lt; & lt; "$ jsonStr" Change the value of the existing property "key1" to "new-value1": jq '.key1 = "new-value1"' <
If you place an JSON file (conceptually speaking) by using the " Key3 "deletion example, update: < / P> # create a test file echo '{"key1": "value1", "key2": "value2", "key3": "value3"}' & gt; Take the test.json #Remove "key3" and return the results back to test.json (with the result it rests). Jq -c 'del (.key3)' test.json & gt; TMP $$ Jsn & amp; Mv tmp. $$ Json test.json You can not replace the input file directly, so the result is written on a temporary file which successfully changes the input file.
Note that the -c option, which makes compact rather than beautiful printed JSON. For all the options and commands , on the manual
Comments
Post a Comment