I'm trying to read individual values from a JSON feed. Here is an example of the feed data:
{
"sendtoken": "token1",
"bytes_transferred": 0,
"num_retries": 0,
"timestamp": 1414395374,
"queue_time": 975,
"message": "internalerror",
"id": "mailerX",
"m0": {
"binding_group": "domain.com",
"recipient_domain": "hotmail.com",
"recipient_local": "destination",
"sender_domain": "domain.com",
"binding": "mail.domain.com",
"message_id": "C1/34-54876-D36FA645",
"api_credential": "creds",
"sender_local": "localstring"
},
"rejecting_ip": "145.5.5.5",
"type": "alpha",
"message_stage": 3
}
{
"sendtoken": "token2",
"bytes_transferred": 0,
"num_retries": 0,
"timestamp": 1414397568,
"queue_time": 538,
"message": "internal error,
"id": "mailerX",
"m0": {
"binding_group": "domain.com",
"recipient_domain": "hotmail.com",
"recipient_local": "destination",
"sender_domain": "domain.com",
"binding": "mail.domain.com",
"message_id": "C1/34-54876-D36FA645",
"api_credential": "creds",
"sender_local": "localstring"
},
"rejecting_ip": "145.5.5.5",
"type": "alpha",
"message_stage": 3
}
I can't share the actual URL, but the above are the first 2 of roughly 150 results that are displayed if I run
print results
before the
json.loads()
line.
My code:
import urllib2
import json
results = urllib2.urlopen(url).read()
jsondata = json.loads(results)
for row in jsondata:
print row['sendtoken']
print row['recipient_domain']
I'd like output like
token1
hotmail.com
for each entry.
I'm getting this error:
ValueError: Extra data: line 2 column 1 - line 133 column 1 (char 583 - 77680)
I'm far from a Python expert, and this is my first time working with JSON. I've spent quite a bit of time looking on google and Stack Overflow, but I can't find a solution that works for my specific data format.