I’m trying to convert the Json type data to dataframe. I extrated data from following webpage with following code.
import pandas as pd
import requests
import json
url = r'https://www.hkex.com.hk/eng/csm/MonthlyStat/data_tab_monthly_202302e.js?_=1'
response = requests.get(url)
if response.status_code != 200:
exit(1)
text = response.text[response.text.index('=') + 1:]
data = json.loads(text)
However, I come across some problem when I trying to conver the Json to dataframe.
I have tried using following code, but the result seem didn’t change when I change the max_level.
df = pd.json_normalize(data, max_level=2)
It seems different from the JSON I met before. Too many data in one json, I only need style 1 data from all id. I am unsure if I using the right code. I’m the beginner in Json. Thanks.
2
Answers
Actually, I found what I want is to extract the data from JSON. And I finally found the method to take the data from JSON. Here is my code:
Here your data is of type
dict
as thejson.loads
function converts json to the python native datatype.json.normalize
will normalize yourdict
value if there is nesting in it. In your case it is stopping at a point where there is a list value for a key.In this api response, the content value is a list which cannot be normalized as it is a list.
You can read more about this here.