简体   繁体   中英

Converting response data from API into table in Power BI using Python

I use python script to connect to an API. The response received is below. I need to convert this data into a multi-level table to be imported into PowerBI as it has a few levels of data (for example, "rooms" is further split and creates a problem)

The response from the API is as below:

{"data": {"code": "AZ-0001", "date_from": "23/12/2019", "date_to": "25/12/2019", "rsrv_date": "06/12/2019", "rooms": [{"rrid": 4057782, "room_name": "STD", "date_from.......

Then convert this using Python Dataframe. This is my python script to get the data from the API

import requests
import pandas as pd
import xml.etree.ElementTree as et
import json

uspw=('<API KEY>', None)
data= {'rcode': 'AZ-0001'}
response= requests.post('https://kapi.wubook.net/kapi/rsrvs/reservation', data, auth= uspw)

data = response.text
print(data)
#df = pd.read_json(data, orient='split')

from pandas.io.json import json_normalize
jdata = json.loads(data)
df = pd.DataFrame(jdata)


#dg = json_normalize(data)
print (df)

The output received in powerBi table looks like this: 在此处输入图片说明

As you can see, the column rooms data is not split. How can I split this into separate columns as well? Thanks alot in advance.

You'll have to save that json to a file, and then import as a new query (Get Data>Json). Another way is to add these steps to your query, in Advanced Editor:

    jsonParser = Json.Document(#"PreviousStep"[json]{0}),
    #"Converted to Table" = Table.FromList(jsonParser , Splitter.SplitByNothing(), null, null, ExtraValues.Error)
in
    #"Converted to Table"

Make sure to replace #"PreviousStep" by the name of yout previous step.

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