从嵌套的json文件中加载重复键作为列表字典 [英] load duplicate keys from nested json file as dictionary of list

查看:107
本文介绍了从嵌套的json文件中加载重复键作为列表字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种格式的json文件,

I have a json file in this format,

{
  "details": {

    "hawk_branch": {
      "tandem": {
        "value": "4210bnd72"
      }
    },
    "uclif_branch": {
      "tandem": {
        "value": "e2nc712nma89",
        "value": "23s24212",
        "value": "12338cm82",
      }
    }
    }
}

问题是,我需要保留所有value,但是当我使用json.load加载该文件时,我只会得到一个value,这很有意义,因为dict只能保留唯一的keys

The problem is, I need to keep all the value, however when i use json.load to load this file i only get one value, which make sense since dict can keep only unique keys.

这是预期的输出,

{ "hawk_branch": ["4210bnd72"] }
{ "uclif_branch": ["e2nc712nma89" , "23s24212", "12338cm82"] }

我已阅读此答案, Python json解析器允许重复键像这样使用object_pairs_hook

I have read this answer, Python json parser allow duplicate keys to use object_pairs_hook like this,

def parse_object_pairs(pairs):
    return pairs

# f is file
json.load(f, object_pairs_hook=parse_object_pairs)

,但它将整个json文件返回为list.

but it returns entire json file as list.

我认为有可能使用lambda作为object_pairs_hook来做到这一点,但我不明白如何使用它.

I think its possible to do it using lambda as object_pairs_hook but i can't understand how can I use it.

有人可以指导我吗

推荐答案

您可以使用自定义重复键解析器功能,该功能将value键的值转换为列表:

You can use a custom duplicate key resolver function that turns the values of the value keys into a list:

def value_resolver(pairs):
    if all(k == 'value' for k, _ in pairs):
        return [v for _, v in pairs]
    return dict(pairs)

这样:

json.load(f, object_pairs_hook=value_resolver)

返回:

{'details': {'hawk_branch': {'tandem': ['4210bnd72']}, 'uclif_branch': {'tandem': ['e2nc712nma89', '23s24212', '12338cm82']}}}

然后通过将列表转换为具有重复的value键的字典来将新的数据结构转储回原始JSON格式,可以使用自定义的json.JSONEncoder子类:

And to dump the new data structure back to the original JSON format by converting lists to dicts with duplicate value keys, you can use a custom json.JSONEncoder subclass:

class restore_value(json.JSONEncoder):
    def encode(self, o):
        if isinstance(o, dict):
            return '{%s}' % ', '.join(': '.join((json.encoder.py_encode_basestring(k), self.encode(v))) for k, v in o.items())
        if isinstance(o, list):
            return '{%s}' % ', '.join('"value": %s' % self.encode(v) for v in o)
        return super().encode(o)

这样:

d = {'details': {'hawk_branch': {'tandem': ['4210bnd72']}, 'uclif_branch': {'tandem': ['e2nc712nma89', '23s24212', '12338cm82']}}}
print(json.dumps(d, cls=restore_value))

将输出:

{"details": {"hawk_branch": {"tandem": {"value": "4210bnd72"}}, "uclif_branch": {"tandem": {"value": "e2nc712nma89", "value": "23s24212", "value": "12338cm82"}}}}

这篇关于从嵌套的json文件中加载重复键作为列表字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆