Home > Net >  Converting from json to DataFrames?
Converting from json to DataFrames?

Time:07-11

I'm trying to convert JSON to DataFrames.

JSON output from API is like that:

{
  "code": 0,
  "data": {
    "list": [
      {
        "address": "abcdxyz",
        "name": "Jack",
        "shares": "396",
        "amount": "490",
        "active": true
      },
      {
        "adress": "efghklm",
        "name": "Mary",
        "shares": "789",
        "amount": "890",
        "active": true
      }
    ],
    "page": 1,
    "size": 5,
    "maxPage": 1,
    "totalSize": 2
  }
}

When try

response = requests.get(url)
json_data = json.loads(response.text)
df = pd.DataFrame(json_data['data'])

its output like that

   list                   page  size  maxPage  totalSize
0  {'address': 'abcd...    1     5        1          2
1  {'address': 'efgh...    1     5        1          2

How can I reach output like that?:

      address    name  shares  amount active
0     abcdxyz    Jack    396    490    true
1     efghklm    Mary    789    890    true

I've tried several things but I couldn't manage to print right way from list. Thanks in advance

CodePudding user response:

You had to go just one level deeper to access not the overall "data" but the specific "list":

response = requests.get(url)
json_data = json.loads(response.text)
df = pd.DataFrame(json_data['data']['list'])
  • Related