Skip to content

Instantly share code, notes, and snippets.

@yarreg
Created February 21, 2018 18:35
Show Gist options
  • Save yarreg/eee0b218ecde60688a7ac84c74d15fbb to your computer and use it in GitHub Desktop.
Save yarreg/eee0b218ecde60688a7ac84c74d15fbb to your computer and use it in GitHub Desktop.
Get/Set JSON key value
#!/bin/sh
set_json_val() {
sed -e 's!^\(\s*"'"$2"'"\s*:\s*"\?\)[^,"]*!\1'"$3"'!' -i $1
}
get_json_val() {
sed -e 's!\([^\\]\)"!\1!g' -e 's!,$!!' $1 | \
sed -n "s!.*${2}: *\(.*\)!\1!p"
}
echo $(get_json_val file.json key_name)
set_json_val file.json key_name key_value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment