Parth S Rawal
Parth S Rawal

Reputation: 27

Iterate through JSON using Python

I have the following JSON object and I wish to iterate through it:

 {
    "odata.metadata": "https://aaa/_api/$metadata#TimesheetActualData",
    "value": [{
        "PROJ_UID": "39bf6bca-8939-4df6-aa81-8a6c3b23c979",
        "TASK_UID": "48dae88e-59ec-e611-80d4-00155d002a1c",
        "ASSN_UID": "4ac8a599-59ec-e611-80d4-00155d002a1c",
        "RES_UID": "e4a6a30d-5da2-e611-80cd-00155d00b80f",
        "PERIOD_UID": "9a08cd04-7f1c-41f1-9a42-cf9b8b3479ea",
        "TS_UID": null,
        "PROJ_NAME": "001 - Module Training Project 1",
        "TASK_NAME": "Approve new or changed Services",
        "TASK_TREE": "> Sample AMS project schedule > Service Strategy > Service Portfolio Management > Approve new or changed Services",
        "TASK_TYPE": "Billable",
        "TASK_CLIENTID": 9,
        "START": "2017-03-05T00:00:00",
        "END": "2017-03-05T00:00:00",
        "PLANNED_WORK": "0.00",
        "PPM_ACTUAL": "0.00",
        "TS_ACTUAL": null,
        "COMMENT": null,
        "SYNC_FLAG": 0,
        "APPROVAL_FLAG": null,
        "CREATED_DATE": "2017-03-17T12:04:38.53",
        "MOD_DATE": "2017-06-09T01:01:42.177",
        "ToBeDeleted": 0,
        "Incident_No": null,
        "Sync_Mod_Date": "2017-06-08T19:31:40",
        "TASK_START_DATE": "2017-02-14T08:00:00",
        "TASK_FINISH_DATE": "2017-03-10T17:00:00",
        "NON_ACTUAL_TASK_FLAG": 0,
        "InvoiceMasterId": 0,
        "Invoice_Status": "0",
        "Billing_Hours": "0.00",
        "Billing_Hours_Flag": null,
        "Final_Amount": "0.00",
        "ProjectCode": null,
        "WORKDAY": 0
    }, {
        "PROJ_UID": "39bf6bca-8939-4df6-aa81-8a6c3b23c979",
        "TASK_UID": "48dae88e-59ec-e611-80d4-00155d002a1c",
        "ASSN_UID": "4ac8a599-59ec-e611-80d4-00155d002a1c",
        "RES_UID": "e4a6a30d-5da2-e611-80cd-00155d00b80f",
        "PERIOD_UID": "9a08cd04-7f1c-41f1-9a42-cf9b8b3479ea",
        "TS_UID": null,
        "PROJ_NAME": "001 - Module Training Project 1",
        "TASK_NAME": "Approve new or changed Services",
        "TASK_TREE": "> Sample AMS project schedule > Service Strategy > Service Portfolio Management > Approve new or changed Services",
        "TASK_TYPE": "Billable",
        "TASK_CLIENTID": 9,
        "START": "2017-03-06T00:00:00",
        "END": "2017-03-06T00:00:00",
        "PLANNED_WORK": "0.00",
        "PPM_ACTUAL": "0.00",
        "TS_ACTUAL": null,
        "COMMENT": null,
        "SYNC_FLAG": 0,
        "APPROVAL_FLAG": null,
        "CREATED_DATE": "2017-03-17T12:04:38.7",
        "MOD_DATE": "2017-06-09T01:01:42.177",
        "ToBeDeleted": 0,
        "Incident_No": null,
        "Sync_Mod_Date": "2017-06-08T19:31:40",
        "TASK_START_DATE": "2017-02-14T08:00:00",
        "TASK_FINISH_DATE": "2017-03-10T17:00:00",
        "NON_ACTUAL_TASK_FLAG": 0,
        "InvoiceMasterId": 0,
        "Invoice_Status": "0",
        "Billing_Hours": "0.00",
        "Billing_Hours_Flag": null,
        "Final_Amount": "0.00",
        "ProjectCode": null,
        "WORKDAY": 0
    }],
    "odata.nextLink": "https://aaaa/_api/TimesheetActualData?$skip=1000"
 }

But somehow, the code is just not working:

header={'content-type': 'application/json'}
response=requests.get(url,headers=header)
jjj=json.dumps(str(response.content))
d=json.loads(jjj)
print(d['values'])

Upvotes: 0

Views: 1411

Answers (2)

Anton vBR
Anton vBR

Reputation: 18914

change

jjj=json.dumps(str(response.content))
d=json.loads(jjj)

To

d = json.loads(response.text)

What is the difference between 'content' and 'text'

r.text is the content of the response in unicode, and r.content is thecontent of the response in bytes

Upvotes: 0

cs95
cs95

Reputation: 403218

You don't have to explicitly pass response.content to the json module to parse it, since requests has a function to do this:

d = response.json()

You can then iterate over its values:

for value in d['value']:
    ...

Upvotes: 1

Related Questions