简体   繁体   中英

Extract nested JSON values

I am trying to figure out on how to convert the following JSON object in to a dataframe

[
   {
      "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. 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.

From there, you can use the json.loads() method and pass it a some JSON. The loads() method returns a Python dictionary which then lets you access the properties of your JSON object by name. 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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