6

I have a JSON file I need to update a particular value.

{
  "Comment": "comment",
  "test": {
    "enabled": true
  },
  "enabled": true,
  "otherStuff": blah,
  "otherStuff2": blah,
  "otherStuff3": blah,
}

I would like to change the value of the second "enabled" to false. With JQ Parser, I can easily retrieve it with jq '.enabled', but I'm not sure what's the best way to manipulate the JSON.

The JSON is a respond i get from an API and may change in the future, I cannot rely on the line or value before/after.

14

a quick experiment:

$ echo '{
  "Comment": "comment",
  "test": {
    "enabled": true
   },
  "enabled": true,
  "otherStuff": "blah",
  "otherStuff2": "blah",
  "otherStuff3": "blah"
}' |
jq '.enabled=false'
{
  "otherStuff3": "blah",
  "otherStuff2": "blah",
  "otherStuff": "blah",
  "enabled": false,
  "test": {
    "enabled": true
  },
  "Comment": "comment"
}
  • How would you change two keys? – Dirk Gorissen Jul 14 '16 at 7:21
1

I read the question as "in the shell" and not necessarily as "using only bash builtins".

Try jsawk, which allows for manipulation and is scriptable, thought it relies on js as a dependency.

If all you want to do is read a (unique) key from the JSON response, you could (adapted from Brendan OConnor):

curl <destination> | grep -Po '"keyname":.*?[^\\]",'`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.