How to edit a JSON file using shell?

7,700

Solution 1

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))'

Solution 2

I would write the script using Python, which has JSON read/write functions built-in.

Given a file, myfile.json, containing the JSON object in the OP:

#!/usr/bin/env python  # Use the Python interpreter
from json import load, dump  # Import the JSON file read and write functions
with open("myfile.json", "r") as file_object:  # Open the JSON file for reading
    json_data = load(file_object)  # Load the data into a dictionary

new_list = [item for item in json_data["list"] if item["name"] != "APP1"]  # Make a new list without the item that should be removed
json_data["list"] = new_list  # Update the dictionary with the new list

with open("myfile.json", "w") as file_object:  # Open the JSON file for writing and truncate it
    dump(json_data, file_object)  # Write the file back to disk
Share:
7,700

Related videos on Youtube

hackedXD
Author by

hackedXD

Updated on September 18, 2022

Comments

  • hackedXD
    hackedXD almost 2 years

    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.

    • berndbausch
      berndbausch about 3 years
      Look into the jq tool. It's purpose is parsing JSON. The usual UNIX text processing tools are not suitable for context-free languages like JSON.
    • Jerb
      Jerb about 3 years
      What do you (Sidharth) mean by "Editing JSON structure is not an option"?
    • Aaron F
      Aaron F about 3 years
      use python - it's a scripting language that you can write shell scripts with: #!/usr/bin/env python3 - it has a json module with which you can load a json file into a dictionary object, manipulate it, then save it back to a file
  • Barmar
    Barmar about 3 years
    You shouldn't modify a list while iterating over it. See stackoverflow.com/questions/6260089/…
  • Aaron F
    Aaron F about 3 years
    @Barmar good points, thanks, will fix..
  • Aaron F
    Aaron F about 3 years
    @Barmar hopefully that's better now :-)
  • Aaron F
    Aaron F about 3 years
    thanks, anonymous editor, i didn't know you could specify the language after the ```