Python 比较两个json对象并删除元素,然后将结果json与其他json文件进行比较
Python新手: Default.jsonPython 比较两个json对象并删除元素,然后将结果json与其他json文件进行比较,python,json,parsing,dictionary,Python,Json,Parsing,Dictionary,Python新手: Default.json { "name": { "provide": "" }, "test": { "Fail": { "centers": None, "Nearest": 0 }, "far": "", "Meta": null, "Only": false, "Tags": null }, "Session": "", "conf": { "check": "", "Reg": "" }, "Token":
{
"name": {
"provide": ""
},
"test": {
"Fail": {
"centers": None,
"Nearest": 0
},
"far": "",
"Meta": null,
"Only": false,
"Tags": null
},
"Session": "",
"conf": {
"check": "",
"Reg": ""
},
"Token": ""
}
我有一个default.json和remote.json。我试图完成的任务是从remote.json中删除所有json元素,其中remote.json的值与default.json匹配。例如,由于键的缘故,name:{provider=”“}from default.json与remote.json中的名称:{provider=”“}匹配。它应该从remote.json中删除
with open(remote.json) as f:
with open(default.json) as m:
file=json.load(f)
default=json.load(m)
for i in xrange(len(file)):
for key,value in default.items():
#default[key]=value
#a=filter(lambda x: x[""],file.keys())
1.我不知道如何从默认值中获取密钥和值,并将其与文件进行比较?如有任何帮助,将不胜感激
我需要从remote.json中删除元素的原因是,我需要将生成的json与其他json文件“local.json”进行比较。如果我不“删除键、值为“”或null或无的值”,那么remote.json和local.json之间的比较将永远不会相等
2.有没有更好的办法解决这个问题
local.json
{
"Name": "",
"conf": {
"check": "name_prefix_match",
},
"test": {
"Service": "redis",
"Fail": {
"Near": 3
},
"Tags": ""
}
}
JSON示例存在一些问题,因为
None
&False
不是有效的JSON对象(事实也是如此),所以让我们假设我们已经解析了文件,并得到了类似的结果
default_json = {
"name": {
"provide": ""
},
"test": {
"Fail": {
"centers": None,
"Nearest": 0
},
"far": "",
"Meta": None,
"Only": False,
"Tags": None
},
"Session": "",
"conf": {
"check": "",
"Reg": ""
},
"Token": ""
}
remote_json = [{
"name": {
"provide": ""
},
"Name": "abc",
"test": {
"Service": "redis",
"Tags": [
"stage"
],
"Fail": {
"centers": None,
"Nearest": 3
},
"Only": False,
"far": "",
"Meta": None
},
"Token": "",
"Session": "",
"conf": {
"Reg": "",
"check": "name_prefix_match"
},
}]
假设remote.json
是字典列表&应该使用default.json
过滤掉每一个字典:
filtered_remote_json = [dict(item
for item in dictionary.items()
if item not in default_json.items())
for dictionary in remote_json]
会给我们
filtered_remote_json == [{"Name": "abc",
"test": {"Service": "redis", "Tags": ["stage"],
"Fail": {"centers": None,
"Nearest": 3}, "Only": False,
"far": "", "Meta": None},
"conf": {"Reg": "",
"check": "name_prefix_match"}}]
filtered_remote_json == [{"Name": "abc",
"test": {"Service": "redis", "Tags": ["stage"],
"Fail": {"Nearest": 3}},
"conf": {"check": "name_prefix_match"}}]
编辑
若我们也需要过滤子字典,那个么下一个有点讨厌的实用程序函数应该会有所帮助
def filter_defaults(json_object, default_json_object):
result = {}
for key, value in json_object.items():
try:
default_value = default_json_object[key]
except KeyError:
# key not in defaults, adding to result
result[key] = value
continue
# we need to process sub-dictionaries as well
if isinstance(value, dict):
value = filter_defaults(value, default_value)
# we are not interested in empty filtered sub-dictionaries
if not value:
continue
# value should differ from default
elif value == default_value:
continue
result[key] = value
return result
那就写吧
filtered_remote_json = [filter_defaults(dictionary, default_json)
for dictionary in remote_json]
这将给我们
filtered_remote_json == [{"Name": "abc",
"test": {"Service": "redis", "Tags": ["stage"],
"Fail": {"centers": None,
"Nearest": 3}, "Only": False,
"far": "", "Meta": None},
"conf": {"Reg": "",
"check": "name_prefix_match"}}]
filtered_remote_json == [{"Name": "abc",
"test": {"Service": "redis", "Tags": ["stage"],
"Fail": {"Nearest": 3}},
"conf": {"check": "name_prefix_match"}}]
JSON示例存在一些问题,因为
None
&False
不是有效的JSON对象(事实也是如此),所以让我们假设我们已经解析了文件,并得到了类似的结果
default_json = {
"name": {
"provide": ""
},
"test": {
"Fail": {
"centers": None,
"Nearest": 0
},
"far": "",
"Meta": None,
"Only": False,
"Tags": None
},
"Session": "",
"conf": {
"check": "",
"Reg": ""
},
"Token": ""
}
remote_json = [{
"name": {
"provide": ""
},
"Name": "abc",
"test": {
"Service": "redis",
"Tags": [
"stage"
],
"Fail": {
"centers": None,
"Nearest": 3
},
"Only": False,
"far": "",
"Meta": None
},
"Token": "",
"Session": "",
"conf": {
"Reg": "",
"check": "name_prefix_match"
},
}]
假设remote.json
是字典列表&应该使用default.json
过滤掉每一个字典:
filtered_remote_json = [dict(item
for item in dictionary.items()
if item not in default_json.items())
for dictionary in remote_json]
会给我们
filtered_remote_json == [{"Name": "abc",
"test": {"Service": "redis", "Tags": ["stage"],
"Fail": {"centers": None,
"Nearest": 3}, "Only": False,
"far": "", "Meta": None},
"conf": {"Reg": "",
"check": "name_prefix_match"}}]
filtered_remote_json == [{"Name": "abc",
"test": {"Service": "redis", "Tags": ["stage"],
"Fail": {"Nearest": 3}},
"conf": {"check": "name_prefix_match"}}]
编辑
若我们也需要过滤子字典,那个么下一个有点讨厌的实用程序函数应该会有所帮助
def filter_defaults(json_object, default_json_object):
result = {}
for key, value in json_object.items():
try:
default_value = default_json_object[key]
except KeyError:
# key not in defaults, adding to result
result[key] = value
continue
# we need to process sub-dictionaries as well
if isinstance(value, dict):
value = filter_defaults(value, default_value)
# we are not interested in empty filtered sub-dictionaries
if not value:
continue
# value should differ from default
elif value == default_value:
continue
result[key] = value
return result
那就写吧
filtered_remote_json = [filter_defaults(dictionary, default_json)
for dictionary in remote_json]
这将给我们
filtered_remote_json == [{"Name": "abc",
"test": {"Service": "redis", "Tags": ["stage"],
"Fail": {"centers": None,
"Nearest": 3}, "Only": False,
"far": "", "Meta": None},
"conf": {"Reg": "",
"check": "name_prefix_match"}}]
filtered_remote_json == [{"Name": "abc",
"test": {"Service": "redis", "Tags": ["stage"],
"Fail": {"Nearest": 3}},
"conf": {"check": "name_prefix_match"}}]
顺便说一句,您的JSON无效:
None
不是null
和False
不是False
子字典也应该被过滤?请,添加过滤远程JSONadded修复的示例,假设在示例中过滤子字典,如'test'
,那么您的JSON无效:None
不是null
并且False
不是False
子字典也应该被过滤?请,在示例中添加过滤远程JSONadded修复的示例,假设过滤子字典(如'test'
)非常感谢您的详细回答。有一点是,过滤的\u remote\u json不应包含值为None或False或“”的键在它里面。我应该如何重构列表理解呢?@immrsteel:那么你也需要过滤子字典吗?Azat Ibrakov感谢Hanks给出了详细的答案。有一点是,过滤的_remote _json不应该有值为None或False或“”的键在它里面。我应该如何重构列表理解呢?@immrsteel:所以你也需要过滤子词典?阿扎特·伊布拉科夫,谢谢