Convert JSON API Requests response to Pandas Dataframe

  Kiến thức lập trình

I’m struggling to convert a JSON API response into a pandas Dataframe object. I’ve read answers to similar questions/documentation, but nothing has helped.

Below is part of the function I’m trying to create:

Below is the response when calling the API:

    'applicantId': '1006086625',
    'refApplicantId': None,
    'CompanyTaxStatus': {
        'RFC': 'XXXX000000X00',
        'PayingTax': 'POSITIVO',
        'ReportDataUpdateDate': '2023-08-02T00: 00: 00'
    'CompanyObligationsList': None,
    'Success': True,
    'message': None
    "applicantId": "1006086625",
    "refApplicantId": null,
    "CompanyTaxStatus": {
        "RFC": "XXXX000000X00",
        "PayingTax": "POSITIVO",
        "ReportDataUpdateDate": "2023-08-02T00:00:00"
    "CompanyObligationsList": null,
    "Success": true,
    "message": null

What I would like to get is only the info from “CompanyTaxStatus” into a Pandas Dataframe.

def get_Company_Tax_Status(company_id):

    url = f"{company_id}"

    headers = {
        'Content-Type': 'application/json',
        'apiId': 'XXXXXXXXXXXXX',
        'apiKey': 'XXXXXXXXXXXXX',
        'Accept-Encoding': 'gzip, deflate'

    response = requests.request("GET", url, headers=headers)

    if response.status_code == 200:
        data = response.json()
        print("JSON Response: n", data)

        df = pd.read_json(data, orient='records')

        print(df.head())  #

        print(f"Failed to fetch data: {response.status_code}")


New contributor

Jorge Fernández is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.