我有一个json对象(json字符串(,它的值如下:
[
{
"id": 1,
"object_k_id": "",
"object_type": "report",
"object_meta": {
"source_id": 0,
"report": "Customers"
},
"description": "Daily metrics for all customers",
"business_name": "",
"business_logic": "",
"owners": [
"nn@abc.com",
null
],
"stewards": [
"nn@abc.com",
''
],
"verified_use_cases": [
null,
null,
"c4a48296-fd92-3606-bf84-99aacdf22a20",
null
],
"classifications": [
null
],
"domains": []
}
]
但是,我想要的最终格式是删除了null和空列表项的格式:类似于这样的格式:
[
{
"id": 1,
"object_k_id": "",
"object_type": "report",
"object_meta": {
"source_id": 0,
"report": "Customers"
},
"description": "Daily metrics for all customers",
"business_name": "",
"business_logic": "",
"owners": [
"nn@abc.com"
],
"stewards": [
"nn@abc.com"
],
"verified_use_cases": [
"c4a48296-fd92-3606-bf84-99aacdf22a20"
],
"classifications": [],
"domains": []
}
]
我希望输出排除null和空字符串,并使其看起来更干净。我需要对我拥有的所有json中的所有列表递归地执行此操作。
除了递归,如果我能一次性完成它,而不是循环遍历每个元素,那将是非常有帮助的。
不过我只需要清理一下清单。
有人能帮我吗?提前感谢
import json
def recursive_dict_clean(d):
for k, v in d.items():
if isinstance(v, list):
v[:] = [i for i in v if i]
if isinstance(v, dict):
recursive_dict_lookup(v)
data = json.loads("""[{
"id": 1,
"object_k_id": "",
"object_type": "report",
"object_meta": {
"source_id": 0,
"report": "Customers"
},
"description": "Daily metrics for all customers",
"business_name": "",
"business_logic": "",
"owners": [
"nn@abc.com",
null
],
"stewards": [
"nn@abc.com"
],
"verified_use_cases": [
null,
null,
"c4a48296-fd92-3606-bf84-99aacdf22a20",
null
],
"classifications": [
null
],
"domains": []
}]""")
for d in data:
recursive_dict_clean(d)
print(data):
[{'id': 1,
'object_k_id': '',
'object_type': 'report',
'object_meta': {'source_id': 0, 'report': 'Customers'},
'description': 'Daily metrics for all customers',
'business_name': '',
'business_logic': '',
'owners': ['nn@abc.com'],
'stewards': ['nn@abc.com'],
'verified_use_cases': ['c4a48296-fd92-3606-bf84-99aacdf22a20'],
'classifications': [],
'domains': []}]
附言:您的json字符串无效。
您可以将json
转换为dict
,然后使用下面的function
并再次将其转换为json
:
def clean_dict(input_dict):
output = {}
for key, value in input_dict.items():
if isinstance(value, dict):
output[key] = clean_dict(value)
elif isinstance(value, list):
output[key] = []
for item in value:
if isinstance(value, dict):
output[key].append(clean_dict(item))
elif value not in [None, '']:
output[key].append(item)
else:
output[key] = value
return output
感谢N.O
您可以在从字符串解码数据时使用内置的object_pairs_hook
来解析数据。
https://docs.python.org/3/library/json.html#json.load
每当解码器调用dict()
时,该函数都会运行,并使用简单的列表理解从列表中删除所有None
对象,否则只保留数据,让解码器自行处理。
#!/usr/bin/env python3
import json
data_string = """[
{
"id": 1,
"object_k_id": "",
"object_type": "report",
"object_meta": {
"source_id": 0,
"report": "Customers"
},
"description": "Daily metrics for all customers",
"business_name": "",
"business_logic": "",
"owners": [
"nn@abc.com",
null
],
"stewards": [
"nn@abc.com",
""
],
"verified_use_cases": [
null,
null,
"c4a48296-fd92-3606-bf84-99aacdf22a20",
null
],
"classifications": [
null
],
"domains": []
}
]"""
def json_hook(obj):
return_obj = {}
for k, v in obj:
if isinstance(v, list):
v = [x for x in v if x is not None]
return_obj[k] = v
return return_obj
data = json.loads(data_string, object_pairs_hook=json_hook)
print(json.dumps(data, indent=4))
结果:
[
{
"id": 1,
"object_k_id": "",
"object_type": "report",
"object_meta": {
"source_id": 0,
"report": "Customers"
},
"description": "Daily metrics for all customers",
"business_name": "",
"business_logic": "",
"owners": [
"nn@abc.com"
],
"stewards": [
"nn@abc.com",
""
],
"verified_use_cases": [
"c4a48296-fd92-3606-bf84-99aacdf22a20"
],
"classifications": [],
"domains": []
}
]
在您的示例中,您从stewards
中删除了""
值,如果您想要这种行为,您可以将is not None
替换为not in (None, "")
。。但这似乎是一个错误,因为你在其他地方留下了空字符串。