2017-05-27 52 views
0

Python的新手:比较两个JSON对象和删除的元素,然后所得的JSON比较其他JSON文件

Default.json

{ 
"name": { 
"provide": "" 
}, 
"test": { 
    "Fail": { 
    "centers": None, 
    "Nearest": 0 
    }, 
    "far": "", 
    "Meta": null, 
    "Only": false, 
    "Tags": null 
}, 
"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': null 
    }, 
    'Token': '', 
    'Session': '', 

    'conf': { 
    'Reg': '', 
    'check': 'name_prefix_match' 
    }, 
} ] 

我有默认.json远程.json。任务我试图实现的是删除所有的json元素remote.json为谁remote.json的值匹配与自该密钥的default.json。例如,名称的值:从default.json {提供商=“”}名称匹配:{供应商= “”}remote.json。它应该得到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.I删除我没有得到这个想法这里如何获得密钥,价值从默认并与文件比较?任何帮助,将不胜感激。

我需要从remote.json中删除元素的原因是因为我需要将生成的json与其他json文件“local.json”进行比较。如果我不删除键值,值为“”或null或没有,那么remote.json和local.json之间的比较永远不会相等。

2.是否有更好的方法去解决这个问题?

本地。JSON

{ 
    "Name": "", 
    "conf": { 
    "check": "name_prefix_match", 
    }, 
    "test": { 
    "Service": "redis", 
    "Fail": { 
    "Near": 3 
    }, 
    "Tags": "" 
    } 
} 
+1

BTW您的JSON是无效的:'None'不是'null'和'FALSE'不'FALSE' –

+0

子词典也应该被过滤? –

+0

请添加已过滤的远程JSON示例 –

回答

2

有一些问题,因为None & False JSON例子不是有效的JSON对象(等等都是single-quoted string literals),所以让“假装我们”已经已经被解析文件,并得到了类似

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"}}] 

编辑

如果我们需要过滤子字典为好,那么下一个有点讨厌的效用函数应该帮助

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": {"Nearest": 3}}, 
          "conf": {"check": "name_prefix_match"}}] 
+0

感谢您的详细解答。filtered_remote_json不应具有无或错误或“”在其中。我应该如何重构列表理解? – immrsteel

+0

@immrsteel:所以你还需要过滤子字典? –

+0

Azat Ibrakov谢谢 – immrsteel