Как редактировать файл JSON с помощью оболочки?

Файловый менеджер Ranger, включающий предварительный просмотр изображения -в настройках.Ranger file manager with image preview

4
16.05.2021, 23:53
2 ответа

Использование функции del в jq:

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

Если вы хотите передать имя приложения в качестве переменной оболочки в jq, вы можете использовать опцию--arg:

jq --arg name "$name" 'del(.list[] | select(.name==$name))'
28
28.07.2021, 11:32

Я бы написал сценарий, используя Python, в который встроены функции чтения/записи JSON -.

Дан файл myfile.json, содержащий объект JSON в 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
3
28.07.2021, 11:32

Теги

Похожие вопросы