0

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'])
8
  • Which part is not working? How is it not working? Commented Jul 8, 2017 at 9:49
  • Last line where i am supposed to get the Values Array and after which i should be able to iterate. Commented Jul 8, 2017 at 9:50
  • Use d['value'] Commented Jul 8, 2017 at 9:51
  • print(d['value']) TypeError: string indices must be integers Commented Jul 8, 2017 at 9:51
  • Also, why are you dumping and loading? You can just use d = response.json() Commented Jul 8, 2017 at 9:52

2 Answers 2

1

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']:
    ...
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.