Shell – How to edit a JSON file using shell

jsonsedshellshell-scriptzsh

I am building a shell script that uses a JSON file.

{
  "property1": true,
  "list": [
    {
      "id": 1,
      "name": "APP1"
    },
    {
      "id": 2,
      "name": "APP2"
    }
  ],
  "property2": false
}

I need to use shell script to read the name from the list and remove its parent object from list. Basically I need to remove the object with name APP1 from the list using shell. Editing JSON structure is not an option.

Best Answer

Using the del function in jq:

jq 'del(.list[] | select(.name=="APP1"))'

If you wanted to pass the app name as a shell variable to jq you can use the --arg option:

jq --arg name "$name" 'del(.list[] | select(.name==$name))'
Related Question