1

I have the following code:

import requests
import json
from requests.structures import CaseInsensitiveDict
headers = CaseInsensitiveDict()
from pandas import json_normalize 
import pandas as pd
headers["Accept"] = "application/json"
headers["Content-Type"] = "application/json"
headers['Authorization']= "Bearer token"
url = "https:link.com"
payload="""{
 "GradeIds" : [7,8],
 "ReportStartDate" : "2020年01月25日T00:00:00",
 "ReportEndDate" : "2020年02月27日T00:00:00"
 }"""
response = requests.request( 'POST',url, headers=headers, data=payload)
parsed = json.loads(response.text)
print(parsed)
len(parsed)
df = json_normalize(parsed)

However, the output is a bit messy and the json_normalize function is unable to make it into a clean dataframe. The following is a sample output from the API from print(parsed):

[{'grade': {'id': 7, 'name': 'stuff1', 'type': 'data'}, 'endOfDayPrices': [{'reportDate': '2020-01-27T00:00:00', 'month': '2020-03-01T00:00:00', 'price': 3.9}, {'reportDate': '2020-01-28T00:00:00', 'month': '2020-03-01T00:00:00', 'price': 3.95}, {'reportDate': '2020-01-28T00:00:00', 'month': '2020-03-01T00:00:00', 'price': 1.05}, {'reportDate': '2020-01-29T00:00:00', 'month': '2020-03-01T00:00:00', 'price': 1.1}, {'reportDate': '2020-01-30T00:00:00', 'month': '2020-03-01T00:00:00', 'price': 0.85}}]

When printing df the dataframe I get the following:

 endOfDayPrices grade.id grade.name \ 
 0 [{'reportDate': '2020-01-27T00:00:00', 'month'... 7 data
 1 [{'reportDate': '2020-01-27T00:00:00', 'month'... 8 data

When checking the length of this list len(parsed) it says there is only 2, grade and endofDayPrices.

Does anyone know how to unpack this list and have a dataframe that looks like this:

grade reportDate price
7 2020年01月27日T00:00:00 2.3
7 2020年01月28日T00:00:00 3.95
,etc. 
asked Apr 8, 2021 at 18:46

1 Answer 1

1

Considering your json to be:

In [1977]: l = [{'grade': {'id': 7, 'name': 'stuff', 'type': 'data'}, 'endOfDayPrices': [{'reportDate': '2020-01-27T00:00:00', 'month': '2020-03-01T00:00:00', 'price': 2.3}, {'reportDate': '2020-01-28T00:00:00', 'month': '2020-03-01T00:00:00', 'price': 3
 ...: .95}, {'reportDate': '2020-01-29T00:00:00', 'month': '2020-03-01T00:00:00', 'price': 2.5}, {'reportDate': '2020-01-30T00:00:00', 'month': '2020-03-01T00:00:00', 'price': 4.0}]}]

You can do this:

In [2079]: df = pd.DataFrame()
In [2083]: for i in l:
 ...: d1 = {}
 ...: reportDate = []
 ...: price = []
 ...: grade = []
 ...: d1['grade'] = i['grade']['id']
 ...: for j in i['endOfDayPrices']:
 ...: reportDate.append(j['reportDate'])
 ...: price.append(j['price'])
 ...: d1['reportDate'] = reportDate
 ...: d1['price'] = price
 ...: df = df.append(pd.DataFrame(d1))
 ...: 
 ...: 
In [2084]: df
Out[2084]: 
 grade reportDate price
0 7 2020年01月27日T00:00:00 3.900
1 7 2020年01月28日T00:00:00 3.950
2 7 2020年01月29日T00:00:00 4.000
3 7 2020年01月30日T00:00:00 4.000
4 7 2020年01月31日T00:00:00 3.900
5 7 2020年02月03日T00:00:00 3.600
6 7 2020年02月04日T00:00:00 3.700
7 7 2020年02月05日T00:00:00 3.700
8 7 2020年02月06日T00:00:00 3.350
9 7 2020年02月07日T00:00:00 3.400
10 7 2020年02月10日T00:00:00 3.300
11 7 2020年02月11日T00:00:00 3.500
12 7 2020年02月12日T00:00:00 3.500
13 7 2020年02月13日T00:00:00 3.500
14 7 2020年02月14日T00:00:00 3.550
15 7 2020年02月18日T00:00:00 3.350
16 7 2020年02月19日T00:00:00 3.150
17 7 2020年02月20日T00:00:00 3.550
18 7 2020年02月21日T00:00:00 3.554
19 7 2020年02月24日T00:00:00 3.555
20 7 2020年02月25日T00:00:00 3.555
21 7 2020年02月26日T00:00:00 2.900
22 7 2020年02月27日T00:00:00 2.700
0 8 2020年01月27日T00:00:00 1.200
1 8 2020年01月28日T00:00:00 1.050
2 8 2020年01月29日T00:00:00 1.100
3 8 2020年01月30日T00:00:00 0.850
4 8 2020年01月31日T00:00:00 0.900
5 8 2020年02月03日T00:00:00 0.650
6 8 2020年02月04日T00:00:00 0.800
7 8 2020年02月05日T00:00:00 1.250
8 8 2020年02月06日T00:00:00 0.900
9 8 2020年02月07日T00:00:00 0.950
10 8 2020年02月10日T00:00:00 0.800
11 8 2020年02月11日T00:00:00 0.950
12 8 2020年02月12日T00:00:00 0.800
13 8 2020年02月13日T00:00:00 0.850
14 8 2020年02月14日T00:00:00 0.850
15 8 2020年02月18日T00:00:00 0.800
16 8 2020年02月19日T00:00:00 1.000
17 8 2020年02月20日T00:00:00 0.933
18 8 2020年02月21日T00:00:00 1.015
19 8 2020年02月24日T00:00:00 1.021
20 8 2020年02月25日T00:00:00 1.020
21 8 2020年02月26日T00:00:00 0.600
22 8 2020年02月27日T00:00:00 1.000
answered Apr 8, 2021 at 19:06
Sign up to request clarification or add additional context in comments.

2 Comments

it outputs the wrong grade it gives 8 for all of them, do you know why that is?
@MichelleM Please check my updated answer.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.