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"https://service.com/apiservice.svc/GetCompanyTaxStatus?taxId={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())  #

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

    print(response.text)

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.

LEAVE A COMMENT