Удалить несколько вложенных элементов из JSON файла в python - PullRequest
0 голосов
/ 09 июля 2020

У меня есть очень большие JSON файлы, из которых мне нужно удалить два вложенных элемента. Вот усеченный пример файла JSON:

enter image description here

Here is the desired output with the elements "quality_control" and "labeller_id" fully removed:

Удаление вложенного ключа JSON из json файла в python

Удаление ключа JSON из файла в python

Как удалять элементы из словаря во время итерации по нему ?

как удалить json объект, используя python?

Удалить элемент в объекте JSON

, но, похоже, я недостаточно углубляюсь в JSON. Что я пробовал до сих пор:

with open('79.json', 'r') as data_file:
data = json.load(data_file)
for element in data:
    element.pop("labeller_id")
with open('mod_79.json', 'w') as data_file:
    data = json.dump(data, data_file)
with open("79_mod.json", 'w') as dest_file:
with open("79.json", 'r') as source_file:
    for line in source_file:
        element = json.loads(line.strip())
        if "labeller_id" in element:
            del element("labeller_id")
        dest_file.write(json.dumps(element))
with open('79.json', 'r') as data_file:
data = json.load(data_file)
if "labeller_id" in data:
    del data["labeller_id"]
with open('mod_79.json', 'w') as data_file:
    data = json.dump(data, data_file)
keys_to_remove = ["labeller_id", "notes"]
with open('79.json', 'r') as data_file:
    data = json.load(data_file)
for key in keys_to_remove:
    #del data[key]
    data.pop[key]
with open('mod_79.json', 'w') as data_file:
    data = json.dump(data, data_file)
import os
import json
keys_to_remove = ["quality_control", "labeller_id"]
with open('79.json', 'r') as dataFile:
    data = json.load(dataFile)
if undesired in data["targets"]["2ff95ced"]:
    del data["targets"]["2ff95ced"][keys_to_remove]
with open('79_mod.json', 'w') as dataFile:
    data = json.dump(data, dataFile)

Был бы очень признателен за любую помощь

...