0

An example of the JSON is something like

{"version":"0.5363"}

I'd need to be able to inject a new value for version thorugh a variable. I found one solution on stackoverflow which says:

echo '{"version":"0.5363"}' | sed 's/\({"version":"\)[^"]*\("}\)/\1newvalue\2/g'

But i wanted to replace the newvalue through varable i.e

echo '{"version":"0.5363"}' | sed 's/\({"version":"\)[^"]*\("}\)/\1$newvalue\2/g'

But is is replacing the string as

{"version":"$newvalue"}

The above example is refrenced one. Actually my JSON looks like below: "checksum": "sdkjjfj-shbdjfj23"

I wanted to replace the checksum value with another value. As per above I used : sed -i 's/("checksum": ")[^"]*(")/\1$checksumVal\2/g' new.json

The new.json is updated as below, but i wanted the value of that variable. "checksum": "$checksumVal",

Any help would be appreciated.

sush
  • 55
  • 1
  • 6
  • 1
    JSON with `sed` is incorrect and not recommended, use `jq` and format your code with 4 spaces – Inian Apr 13 '18 at 09:13
  • 1
    For posterity, a solution using `jq` - [Modify a key-value in a json using jq](https://stackoverflow.com/q/42716734/5291015) – Inian Apr 13 '18 at 09:20
  • and `jq '.version = "'"$newvalue"'"'` for using variable.. add `-c` option to get compact output – Sundeep Apr 13 '18 at 09:23

0 Answers0