如何从包含具有重复键的对象的JSON文档中保留所有键值对?

2024-09-28 21:21:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我在正确完成这项工作时遇到了一些困难,但我有如下数据:

{  
      "completedProtocol": "Extract",
      "map": [
        {
          "sampleIDsIn": [{ "clarityId": "claritySample1", "espId": "ESP024254" }, { "clarityId": "claritySample1", "espId": "ESP024255" }, { "clarityId": "claritySample1", "espId": "ESP024256"}],
          "sampleIDsOut": ["claritySample3", "claritySample4", "claritySample5"],
          "files":["http://fileserver.net/path/to/datafile3"]
        }
      ],
      "map": [
        {
          "sampleIDsIn": [{ "clarityId": "claritySample1", "espId": "ESP024258" }, { "clarityId": "claritySample1", "espId": "ESP024259" }, { "clarityId": "claritySample1", "espId": "ESP024260"}],
          "sampleIDsOut": ["claritySample3", "claritySample4", "claritySample5"],
          "files":["http://fileserver.net/path/to/datafile3"]
        }
      ]
    }

我想把它转换成:

[{"map": [
        {
          "sampleIDsIn": [{ "clarityId": "claritySample1", "espId": "ESP024254" }, { "clarityId": "claritySample1", "espId": "ESP024255" }, { "clarityId": "claritySample1", "espId": "ESP024256"}],
          "sampleIDsOut": ["claritySample3", "claritySample4", "claritySample5"],
          "files":["http://fileserver.net/path/to/datafile3"]
        }
      ]},
{"map":[
        {
          "sampleIDsIn": [{ "clarityId": "claritySample1", "espId": "ESP024258" }, { "clarityId": "claritySample1", "espId": "ESP024259" }, { "clarityId": "claritySample1", "espId": "ESP024260"}],
          "sampleIDsOut": ["claritySample3", "claritySample4", "claritySample5"],
          "files":["http://fileserver.net/path/to/datafile3"]
        }
      ]}]

到目前为止,我的代码是:

import json

obj = json.loads(body)
newData = [dct for dct in obj if 'map' in dct]

但这只会带来:

[u'map']

如果我在主体上使用json.loads,它只返回map的第二个值,覆盖第一个值。你知道吗

注意:我想要一系列的单项dict;我不想在单个键下收集值。你知道吗

有什么想法吗?你知道吗


Tags: topathhttpmapnetfilesfileserversampleidsin
1条回答
网友
1楼 · 发布于 2024-09-28 21:21:50

您可以使用自定义object_pairs_hook函数强制json.loads()返回单个项目dict的列表,而不是覆盖重复键的单个dict:

import json

def keep_duplicates(ordered_pairs):
    result = []
    for key, value in ordered_pairs:
        result.append({key: value})
    return result

docs

object_pairs_hook is an optional function that will be called with the result of any object literal decoded with an ordered list of pairs. The return value of object_pairs_hook will be used instead of the dict. This feature can be used to implement custom decoders that rely on the order that the key and value pairs are decoded (for example, collections.OrderedDict() will remember the order of insertion). If object_hook is also defined, the object_pairs_hook takes priority.

用法:

>>> json.loads('{"a": 1, "a": 2, "a": 3}', object_pairs_hook=keep_duplicates)
[{u'a': 1}, {u'a': 2}, {u'a': 3}]

在您的例子中,由于您显然对"map"键以外的任何东西都不感兴趣,因此可以在之后过滤结果:

all_data = json.loads(body, object_pairs_hook=keep_duplicates)
map_data = [x for x in all_data if 'map' in x]

…这会让你得到你问题中指定的结果。你知道吗

相关问题 更多 >