0

I am trying to convert a JSON file to a dataframe. The JSON file has multiple children but the dataframe puts everything into one row.

I have pasted the python code and JSON file itself below - how can I have one row for all the attributes - one row for "gender", another for "name" etc?

What is the proper way to parse these JSON files into dataframe? JSON files could be simple files or heavily nested - what is the best recommendation ?

import requests
import pandas as pd
import json
from pandas import json_normalize

requestObject=requests.get("https://randomuser.me/api")
#print(requestObject)
requestObject_JSON=requestObject.json()
print(requestObject_JSON)
requestObject_JSON_DF=pd.DataFrame(requestObject_JSON['results'])
print(requestObject_JSON_DF)

JSON text

{
"results": [
    {
        "gender": "female",
        "name": {
            "title": "Miss",
            "first": "Gül",
            "last": "Özbey"
        },
        "location": {
            "street": {
                "number": 7811,
                "name": "Talak Göktepe Cd"
            },
            "city": "Karaman",
            "state": "Uşak",
            "country": "Turkey",
            "postcode": 56232,
            "coordinates": {
                "latitude": "65.5346",
                "longitude": "17.3572"
            },
            "timezone": {
                "offset": "+5:45",
                "description": "Kathmandu"
            }
        },
        "email": "gul.ozbey@example.com",
        "login": {
            "uuid": "8f2cc4dc-872d-4be3-9c71-f201c29a9aaf",
            "username": "bigwolf266",
            "password": "hurrican",
            "salt": "Ek2Q6oJX",
            "md5": "08b865af0d45ec95386ee2e3ce1102e3",
            "sha1": "e9a12f61bee5aef0151d4a837ce7205e7d05f961",
            "sha256": "083ba51470815536f5b85973e5b42694defb2e7c88ba1186e15f600cc00f5f16"
        },
        "dob": {
            "date": "1945-10-22T10:41:53.896Z",
            "age": 77
        },
        "registered": {
            "date": "2003-07-21T22:45:56.710Z",
            "age": 20
        },
        "phone": "(156)-666-9612",
        "cell": "(720)-437-5024",
        "id": {
            "name": "",
            "value": null
        },
        "picture": {
            "large": "https://randomuser.me/api/portraits/women/74.jpg",
            "medium": "https://randomuser.me/api/portraits/med/women/74.jpg",
            "thumbnail": "https://randomuser.me/api/portraits/thumb/women/74.jpg"
        },
        "nat": "TR"
    }
],
"info": {
    "seed": "18cfa12987545012",
    "results": 1,
    "page": 1,
    "version": "1.4"
}

}

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Nidhin_toms
  • 707
  • 4
  • 18
  • 29
  • 1
    There are already **many** questions dealing with this. [tag:json_normalize] This question doesn't show any attempt to implement the many existing options already on SO. – Trenton McKinney Aug 26 '23 at 22:55

0 Answers0