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 can I add and remove a new key (i.e "key4": "value4"
) by bash script? I see also the issue to add or remove a comma at the end of last key in the file before adding or removing the new one.
Thank you
In the Enterprise Explorer view, right-click your . json file or other file type that contains JSON code and select Open With > JSON Editor. You can compress JSON strings so that the strings display on one line with white space removed between JSON elements.
Use grep to Parse JSON in Bash grep command can also be used for parsing JSON data. We use the -o option to select only lines that match the given pattern. Then, we specify the pattern '"email": "[^"]*' , which means we want all of the values of the key email .
Vim is a file opener software that can be used to open the JSON file on Linux platform. GitHub Atom is a cross-platform tool to open JSON files. Other than these tools, you can use web browsers like Google Chrome and Mozilla Firefox to open JSON files, which we discuss in detail later.
Your best bet is to use a JSON CLI such as jq
:
sudo apt-get install jq
brew install jq
Examples, based on the following input string - output is to stdout
:
jsonStr='{ "key1": "value1", "key2": "value2", "key3": "value3" }'
jq 'del(.key3)' <<<"$jsonStr"
jq '. + { "key4": "value4" }' <<<"$jsonStr"
jq '.key1 = "new-value1"' <<<"$jsonStr"
A more robust alternative thanks, Lars Kiesow :
If you pass the new value with --arg
, jq
takes care of properly escaping the value:
jq '.key1 = $newVal' --arg newVal '3 " of rain' <<<"$jsonStr"
If you want to update a JSON file in place (conceptually speaking), using the example of deleting "key3":
# Create test file. echo '{ "key1": "value1", "key2": "value2", "key3": "value3" }' > test.json # Remove "key3" and write results back to test.json (recreate it with result). jq -c 'del(.key3)' test.json > tmp.$$.json && mv tmp.$$.json test.json
You cannot replace the input file directly, so the result is written to a temporary file that replaces the input file on success.
Note the -c
option, which produces compact rather than pretty-printed JSON.
For all options and commands, see the manual at http://stedolan.github.io/jq/manual/.
Not the answer for everyone, but if you already happen to have NodeJs installed in your system, you can use it to easily manipulate JSON.
eg:
#!/usr/bin/env bash jsonFile=$1; node > out_${jsonFile} <<EOF //Read data var data = require('./${jsonFile}'); //Manipulate data delete data.key3 data.key4 = 'new value!'; //Output data console.log(JSON.stringify(data)); EOF
Heck, if you only need to do JSON manipulation and you have node (ie: You don't really need any other bash functionality) you could directly write a script using node as the interpreter:
#! /usr/bin/env node var data = require('./'+ process.argv[2]); /*manipulate*/ console.log(JSON.stringify(data));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With