删除对象数组中的JSON属性 [英] Removing JSON property in array of objects
本文介绍了删除对象数组中的JSON属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个用Python清理的JSON数组.我要删除imageData
属性:
I have a JSON array that I'm cleaning up in Python. I want to remove the imageData
property:
data.json
[{"title": "foo", "imageData": "xyz123"},
{"title": "bar", "imageData": "abc123"},
{"title": "baz", "imageData": "def456"}]
我正在设置一个列表理解以删除该属性,但是我不确定如何创建专注于imageData
的变量:
I am setting up a list comprehension to remove the property, but I'm not sure how to create the variable that focuses on imageData
:
import json
with open('data.json') as json_data:
data = json.load(json_data)
clean_data = [ item for item in data if not item['imageData'] ]
# Write `clean_data` to new json file
当我print
列表理解时,它返回一个空数组.我必须纠正什么才能使其正常工作?
When I print
the list comprehension, it returns an empty array. What do I have to correct to get this working properly?
推荐答案
一个简单的解决方案是使用del
删除就位的不需要的密钥:
An easy solution to your problem is deleting the unwanted key in place, with del
:
import json
with open('data.json') as json_data:
data = json.load(json_data)
for element in data:
del element['imageData']
您应该添加一些安全检查,但是您明白了.
You should add some safety checks, but you get the idea.
这篇关于删除对象数组中的JSON属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文