0

I am trying to decode a JSON with Python. Here is a little snippet of what the JSON looks like.

b'{"success":true,"data":[{"id":26,"name":"A","comment":"","start_time_plan":null,"start_time_actual":"2016-09-13 00:00:00","start_time_delta":null,"start_time_score":null,"start_time_score_achievement":null,"start_time_traffic_light":null,"end_time_plan":null,"end_time_actual":"2016-09-13 00:00:00","end_time_delta":null,"end_time_score":null,"end_time_score_achievement":null,"end_time_traffic_light":null,"status":0,"measure_schedule_revision_id":63,"responsible_user_id":3,"created_time":"2016-09-13 11:29:14","created_user_id":3,"modified_time":"2016-09-21 16:33:41","modified_user_id":3,"model":"Activity"},{"id":27,"name":"B","comment":"","start_time_plan":null,"start_time_actual":"2016-09-13 00:00:00","start_time_delta":null,"start_time_score":null,"start_time_score_achievement":null,"start_time_traffic_light":null,"end_time_plan":null,"end_time_actual":"2016-09-13 00:00:00","end_time_delta":null,"end_time_score":null,"end_time_score_achievement":null,"end_time_traffic_light":null,"status":0,"measure_schedule_revision_id":63,"responsible_user_id":3,"created_time":"2016-09-13 11:29:48","created_user_id":3,"modified_time":"2016-10-16 18:14:36","modified_user_id":1,"model":"Activity"}

I am trying to get a hold of start_time_deltaand end_time_delta and produce a little scatter plot. But somehow I can't decode the JSON.

Here is what I do:

#falcon api
u = 'https://myurl.com'
#urllib3 + poolmanager for requests
import urllib3
http = urllib3.PoolManager()
import json
r = http.request('GET', u)
json.loads(r.data.decode('utf-8'))
end = json.loads(r.data['end_time_delta'])
start = json.loads(r.data['start_time_delta'])

This is the error I get: byte indices must be integers or slices, not str

How come? And how do I solve the problem?

Martijn Pieters
1.1m326 gold badges4.2k silver badges3.5k bronze badges
asked Nov 27, 2016 at 16:14
1
  • Also consider using the requests module, which automatically handles pooling and provides a very simple way to parse the JSON returned by a request. Commented Nov 27, 2016 at 16:32

1 Answer 1

4

You are ignoring the return value of json.loads() here:

json.loads(r.data.decode('utf-8'))

You then try to decode the same raw again and try to use that as the decoded Python result. Call json.loads() once, and use the resulting Python dictionary:

result = json.loads(r.data.decode('utf-8'))
start = result['data'][0]['start_time_delta']
end = result['data'][0]['end_time_delta']

Because top-level dictionary 'data' key points to a list of results, I used 0 to get to the first of those and extract the data you want.

If you need to extract those data points for every dictionary in that list, you'd have to use a loop:

for entry in result['data']:
 start = entry['start_time_delta']
 end = entry['end_time_delta']
 # do something with these two values, before iterating to the next
answered Nov 27, 2016 at 16:16
Sign up to request clarification or add additional context in comments.

2 Comments

Ah...I am such a fool! Thank you! I guess I can now loop through the JSON from 0 to n?
@Rachel: Python loops are foreach constructs, no need to use indices when you can just access the contained elements of a sequence directly.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.