1

I am partially able to work with json saved as file:

#! /usr/bin/python3

import json
from pprint import pprint

json_file='a.json'
json_data=open(json_file)
data = json.load(json_data)
json_data.close()
print(data[10])

But I am trying to achieve the same from data directly from web. I am trying with the accepted answer here:

#! /usr/bin/python3

from urllib.request import urlopen
import json
from pprint import pprint

jsonget=urlopen("http://api.crossref.org/works?query.author=Rudra+Banerjee")

data = json.load(jsonget)
pprint(data)

which is giving me error:

  Traceback (most recent call last):
  File "i.py", line 10, in <module>
    data = json.load(jsonget)
  File "/usr/lib64/python3.5/json/__init__.py", line 268, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "/usr/lib64/python3.5/json/__init__.py", line 312, in loads
    s.__class__.__name__))
TypeError: the JSON object must be str, not 'bytes'

What is going wrong here?

Changing the code as par Charlie's reply to:

jsonget=str(urlopen("http://api.crossref.org/works?query.author=Rudra+Banerjee"))

data = json.load(jsonget)
pprint(jsonget)

breaks at json.load:

Traceback (most recent call last):
  File "i.py", line 9, in <module>
    data = json.load(jsonget)
  File "/usr/lib64/python3.5/json/__init__.py", line 265, in load
    return loads(fp.read(),
AttributeError: 'str' object has no attribute 'read'
Community
  • 1
  • 1
BaRud
  • 3,055
  • 7
  • 41
  • 89
  • @alecxe: may I humbly ask if you have ran the code (its minimal and complete) before suggesting the duplicate and possibly the close vote? – BaRud Jul 24 '16 at 20:16
  • I apologize if it is not a real duplicate, but it really looks like. I'm open to reopening the question if you can kindly explain why this is a different use case. Thanks. As for your last problem, since you have a string now, use `json.loads()` and not `json.load()`. – alecxe Jul 24 '16 at 20:21
  • @alecxe: loads also fails. Actually that is why I have loaded the complete code and asked(humbly, again) if you have really ran it. If it is a possible duplicate, I am fine with flagging it that way...we all want so clean. But the problem doesnt look straight to me. but then, I have no experience in handling json. – BaRud Jul 24 '16 at 20:26
  • `data = json.loads(jsonget.read().decode())` – Burhan Khalid Jul 24 '16 at 20:39

1 Answers1

-1

It's actually telling you the answer: you're getting back a byte array, where in Python 3 a string is different because of dealing with unicode. In Python 2.7, it would work. You should be able to fix it by converting your bytes explicitly to a string with

jsonget=str(urlopen("http://api.crossref.org/works?query.author=Rudra+Banerjee")_
Charlie Martin
  • 110,348
  • 25
  • 193
  • 263