简体   繁体   English

提取嵌套的 JSON 值

[英]Extract nested JSON values

I am trying to figure out on how to convert the following JSON object in to a dataframe我想弄清楚如何将以下 JSON 对象转换为数据帧

[
   {
      "id":"123",
      "sources":[
         {
            "name":"ABC",
            "first":"2020-02-26T03:19:23.247Z",
            "last":"2020-02-26T03:19:23.247Z"
         },
         {
            "name":"XYZ",
            "first":"2020-02-26T03:19:23.247Z",
            "last":"2020-02-26T03:19:23.247Z"
         }
      ]
   }
]


The dataframe should appear like this.
id       ABC.first         ABC.last        XYZ.first                 XYZ.last
123      2020-02-26..      2020-02-26..   2020-02-26..  2020-02-26T03:19:23.247Z

Thanks in advance for your help在此先感谢您的帮助

Python includes the useful json module. Python 包含有用的json模块。 You can find some documentation for it here .您可以在此处找到有关它的一些文档。

To use it , you'll need to import it into your Python script using the import json command.要使用它,您需要使用import json命令将其import json Python 脚本。

From there, you can use the json.loads() method and pass it a some JSON.从那里,您可以使用json.loads()方法并将其传递给一些 JSON。 The loads() method returns a Python dictionary which then lets you access the properties of your JSON object by name. loads()方法返回一个Python 字典,然后您可以通过该字典按名称访问 JSON 对象的属性。 Some example code might look like:一些示例代码可能如下所示:

import json

# some JSON:
personJSON = '{"name":"John", "age":30, "city":"New York", "address": {"street": "Fake Street", "streetNumber": "123"}}'

# parse personJSON:
personDict = json.loads(personJSON)

# the result is a Python dictionary:
print(personDict["name"]) # Will print "John"
print(personDict["address"]["street"]) # Will print "Fake Street"

Good luck!祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM