简体   繁体   中英

how to convert Json response into a pandas dataframe?

I want to convert the JSON response into a pandas dataframe( All Names, All IDs,etc. into a dataframe ) and then save the Pandas dataframe to CSV file; I expect it to be like this: Because the data is bigdata I Do NOT want to use loops. 在此处输入图像描述 I want to do it without For loop . This is my code:

req = urllib.request.Request(url, headers=hdr)
    req.get_method = lambda: 'GET'
    response = urllib.request.urlopen(req)
    ### Response: 200 OK
    print(response.getcode())
    ### JSON Output
    print(response.read())

and this is the JSON response:

200
b'{
    "results": [{
            "accountId": "d85b3704-60c9-474f-aecc-2886629c732e",
            "id": "69fb205b25",
            "partition": null,
            "externalId": null,
            "metadata": null,
            "name": "New",
            "description": null,
            "created": "2022-09-19T17:55:58.244+00:00",
            "lastModified": "2022-09-19T17:56:05.107+00:00",
            "lastIndexed": "2022-09-19T17:56:05.107+00:00",
            "privacyMode": "Private",
            "userName": "Mina M",
            "isOwned": true,
            "isBase": true,
            "hasSourceVideoFile": true,
            "state": "Processing",
            "moderationState": "OK",
            "reviewState": "None",
            "processingProgress": "5%",
            "durationInSeconds": 0,
            "thumbnailVideoId": "69fb205b25",
            "thumbnailId": "00000000-0000-0000-0000-000000000000",
            "searchMatches": [],
            "indexingPreset": "Default",
            "streamingPreset": "Default",
            "sourceLanguage": "en-US",
            "sourceLanguages": ["en-US"],
            "personModelId": "00000000-0000-0000-0000-000000000000"
        }, {
            "accountId": "d85b3704-60c9-474f-aecc-2886629c732e",
            "id": "c3305446db",
            "partition": null,
            "externalId": null,
            "metadata": null,
            "name": "New",
            "description": null,
            "created": "2022-09-19T14:39:28.206+00:00",
            "lastModified": "2022-09-19T14:40:41.714+00:00",
            "lastIndexed": "2022-09-19T14:39:54.108+00:00",
            "privacyMode": "Private",
            "userName": "Mina M",
            "isOwned": true,
            "isBase": true,
            "hasSourceVideoFile": true,
            "state": "Processed",
            "moderationState": "OK",
            "reviewState": "None",
            "processingProgress": "100%",
            "durationInSeconds": 58,
            "thumbnailVideoId": "c3305446db",
            "thumbnailId": "5b8367dd-88d3-4f64-a347-2d0a6239cd79",
            "searchMatches": [],
            "indexingPreset": "Default",
            "streamingPreset": "Default",
            "sourceLanguage": "en-US",
            "sourceLanguages": ["en-US"],
            "personModelId": "00000000-0000-0000-0000-000000000000"
        }, {
            "accountId": "d85b3704-60c9-474f-aecc-2886629c732e",
            "id": "c252e34477",
            "partition": null,
            "externalId": null,
            "metadata": null,
            "name": "New",
            "description": null,
            "created": "2022-09-19T13:28:28.66+00:00",
            "lastModified": "2022-09-19T13:32:02.48+00:00",
            "lastIndexed": "2022-09-19T13:28:40.137+00:00",
            "privacyMode": "Private",
            "userName": "Mina M",
            "isOwned": true,
            "isBase": true,
            "hasSourceVideoFile": true,
            "state": "Processed",
            "moderationState": "OK",
            "reviewState": "None",
            "processingProgress": "100%",
            "durationInSeconds": 58,
            "thumbnailVideoId": "c252e34477",
            "thumbnailId": "fa637059-2770-4e00-9464-bfc16a3b18eb",
            "searchMatches": [],
            "indexingPreset": "Default",
            "streamingPreset": "Default",
            "sourceLanguage": "en-US",
            "sourceLanguages": ["en-US"],
            "personModelId": "00000000-0000-0000-0000-000000000000"
        }, {
            "accountId": "d85b3704-60c9-474f-aecc-2886629c732e",
            "id": "93b00229d9",
            "partition": null,
            "externalId": null,
            "metadata": null,
            "name": "New",
            "description": null,
            "created": "2022-09-19T13:21:23.975+00:00",
            "lastModified": "2022-09-19T13:22:13.147+00:00",
            "lastIndexed": "2022-09-19T13:21:28.134+00:00",
            "privacyMode": "Private",
            "userName": "Mina M",
            "isOwned": true,
            "isBase": true,
            "hasSourceVideoFile": true,
            "state": "Processed",
            "moderationState": "OK",
            "reviewState": "None",
            "processingProgress": "100%",
            "durationInSeconds": 58,
            "thumbnailVideoId": "93b00229d9",
            "thumbnailId": "11faaa20-9318-463c-92c4-38be7bf5ba99",
            "searchMatches": [],
            "indexingPreset": "Default",
            "streamingPreset": "Default",
            "sourceLanguage": "en-US",
            "sourceLanguages": ["en-US"],
            "personModelId": "00000000-0000-0000-0000-000000000000"
        }

How can do it without loops?

You can use:

pandas.json_normalize(
    json_list, 
    record_path =['results'], 
    meta=['id', 'name'] )

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