Access value in nested JSON recieved by ActiveCollab REST API using Python

Question:

I got this by REST-API form ActiveCollab:

{
"time_records": [
        {
            "id": 122,
            "class": "TimeRecord",
            "url_path": "/projects/89/time-records/122",
            "is_trashed": false,
            "trashed_on": null,
            "trashed_by_id": 0,
            "billable_status": 1,
            "value": 1.0,
            "record_date": 1645847200,
            "summary": "example",
            "user_id": 12365,
            "user_name": "Fred Coal",
            "user_email": "[email protected]",
            "parent_type": "Project",
            "parent_id": 89,
            "created_on": 1628497959,
            "created_by_id": 17,
            "created_by_name": "Fred Coal",
            "created_by_email": "[email protected]",
            "updated_on": 1635784512,
            "updated_by_id": 17,
            "job_type_id": 1,
            "source": "unknown",
            "original_is_trashed": false
        },       
    ],
}

I want to get the value of "value".

But I don’t get how to access this property.

I am using Python for this.

import requests
import json
startdate = "2021-08-01"
enddate = "2021-08-31"
fromto = "?from="+startdate+"&to="+enddate
#-------------- 
def ac_rq(endpoint):
    token ='*'
    url = "https://*/api/v1/"+endpoint
    resp = dict(requests.get(url, headers={'X-Angie-AuthApiToken': token}))
    print(resp)
    return resp
#--------------
a = ac_rq('projects/129/time-records/filtered-by-date'+str(fromto))
b = json.loads(a.text)
for i in b:
    print(i['time_records'][0]['value'])

input("press enter to exit")

I get this Error:

TypeError: string indices must be integers

Asked By: Luke

||

Answers:

As @rdas mentions in the comment you can access "value" by this:response['time_records'][0]['value']

Answered By: user15256253

see below (load the json string into a dict and refer to the requested entry)

import json
data = '''{
"time_records": [
        {
            "id": 122,
            "class": "TimeRecord",
            "url_path": "/projects/89/time-records/122",
            "is_trashed": false,
            "trashed_on": null,
            "trashed_by_id": 0,
            "billable_status": 1,
            "value": 1.0,
            "record_date": 1645847200,
            "summary": "example",
            "user_id": 12365,
            "user_name": "Fred Coal",
            "user_email": "[email protected]",
            "parent_type": "Project",
            "parent_id": 89,
            "created_on": 1628497959,
            "created_by_id": 17,
            "created_by_name": "Fred Coal",
            "created_by_email": "[email protected]",
            "updated_on": 1635784512,
            "updated_by_id": 17,
            "job_type_id": 1,
            "source": "unknown",
            "original_is_trashed": false
        }      
    ]
}'''

data = json.loads(data)
print(data['time_records'][0]['value'])

output

1.0
Answered By: balderman
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.