【问题标题】:How to get required json output from complex nested json format.?如何从复杂的嵌套 json 格式中获取所需的 json 输出。?
【发布时间】:2021-04-01 10:02:03
【问题描述】:

我的原始文件是 CSV 格式,我已将其转换为 python JSON 数组到 JSON Sring。 json文件

<class 'list'>
<class 'dict'>

[
  {
    "key": "timestamp",
    "source": "eia007",
    "turnover": "65million",
    "url": "abc.com",
    "record": "",
    "loc.reg": "nord000",
    "loc.count": "abs39i5",
    "loc.town": "cold54",
    "co.gdp": "nscrt77",
    "co.pop.min": "min50",
    "co.pop.max": "max75",
    "co.rev": "",
    "chain.system": "5t5t5",
    "chain.type": "765ef",
    "chain.strat": "",
    
   }
]

我想得到如下输出:

{
 "timestamp001": {
 "key": "timestamp001",
 "phNo": "ner007",
 "turnover": "65million",
 "url": "abc.com",
 "record": "",
 "loc": {
    "reg": "nord000",
    "count": "abs39i5",
    "town": "cold54"
   },
 "co": {
    "form": "nscrt77",
    "pop": {
        "min": "min50",
        "max": "max75"
         },
        "rev: ""
    },
  "chain":{
           "system": "5t5t5",
           "type": "765ef",
           "strat": ""
          }
     ...
      }
...
  }
]

我尝试了不同的选择;试图枚举,但无法获得所需的输出。请帮我解决一下这个。提前致谢。

【问题讨论】:

    标签: json python-3.x string


    【解决方案1】:

    你可以使用这样的东西来创建嵌套字典:

    import json
    
    def unflatten(somedict):
        unflattened = {}
    
        for key, value in somedict.items():
            splitkey = key.split(".")
            print(f"doing {key} {value} {splitkey}")
    
            # subdict is the dict that goes deeper in the nested structure
            subdict = unflattened
    
            for subkey in splitkey[:-1]:
    
                # if this is the first time we see this key, add it
                if subkey not in subdict:
                    subdict[subkey] = {}
    
                # shift the subdict a level deeper
                subdict = subdict[subkey]
    
            # add the value
            subdict[splitkey[-1]] = value
    
        return unflattened
    
    
    data = {
        "key": "timestamp",
        "source": "eia007",
        "turnover": "65million",
        "url": "abc.com",
        "record": "",
        "loc.reg": "nord000",
        "loc.count": "abs39i5",
        "loc.town": "cold54",
        "co.gdp": "nscrt77",
        "co.pop.min": "min50",
        "co.pop.max": "max75",
        "co.rev": "",
        "chain.system": "5t5t5",
        "chain.type": "765ef",
        "chain.strat": "",
    }
    
    unflattened = unflatten(data)
    print(json.dumps(unflattened, indent=4))
    

    产生:

    {
        "key": "timestamp",
        "source": "eia007",
        "turnover": "65million",
        "url": "abc.com",
        "record": "",
        "loc": {
            "reg": "nord000",
            "count": "abs39i5",
            "town": "cold54"
        },
        "co": {
            "gdp": "nscrt77",
            "pop": {
                "min": "min50",
                "max": "max75"
            },
            "rev": ""
        },
        "chain": {
            "system": "5t5t5",
            "type": "765ef",
            "strat": ""
        }
    }
    

    干杯!

    【讨论】:

    • 谢谢。这是在处理较小的数据子集;如果我添加更多数据,则会抛出 TypeError: ('str' object does not support item assignment)。我想是因为字符串不变性?
    • 不,你可能有这样的字典 {"a": "val", "a.b": "val"} 我猜,所以嵌套结构是不明确的:添加了键 a,其值是一个字符串,但随后 @ 987654325@ 被解析并且程序尝试添加一个子键b(因为此时a 确实存在)但它失败了,因为unflattened['a'] 是一个字符串而不是一个字典。您需要明确决定如何处理此类案件。
    猜你喜欢
    • 2017-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-20
    • 2018-02-02
    • 2016-08-07
    • 1970-01-01
    相关资源
    最近更新 更多