1

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

3
  • 3
    Try df = pd.DataFrame(json_data['data']['list']) Commented Jul 10, 2022 at 21:39
  • Jerzy Pawlikowski is right. If the order of columns matters to you, the following should work: df = pd.DataFrame(json_data['data']['list'])[['address', 'name', 'shares', 'amount', 'active']] Commented Jul 10, 2022 at 21:42
  • @JerzyPawlikowski thanks for that. I can't believe that it was easy solution :)) Thanks a lot. Commented Jul 10, 2022 at 21:47

1 Answer 1

0

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'])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.