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
Related videos on Youtube
![hackedXD](https://i.stack.imgur.com/hJZJv.png?s=256&g=1)
Author by
hackedXD
Updated on September 18, 2022Comments
-
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 thelist
and remove its parent object from list. Basically I need to remove the object with nameAPP1
from thelist
using shell. Editing JSON structure is not an option.-
berndbausch about 3 yearsLook 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 about 3 yearsWhat do you (Sidharth) mean by "Editing JSON structure is not an option"?
-
Aaron F about 3 yearsuse 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 about 3 yearsYou shouldn't modify a list while iterating over it. See stackoverflow.com/questions/6260089/…
-
Aaron F about 3 years@Barmar good points, thanks, will fix..
-
Aaron F about 3 years@Barmar hopefully that's better now :-)
-
Aaron F about 3 yearsthanks, anonymous editor, i didn't know you could specify the language after the ```