0

I have an issue with json on Python3. I try to get the "pairs" from string of URL: https://wex.nz/api/3/info

This is my code:

import urllib.request
import json

url= 'https://wex.nz/api/3/info'
content=urllib.request.urlopen(url)
for line in content:
    liste=json.loads(line)

pairliste=liste['pairs']
print(pairliste)

This is my error:

Traceback (most recent call last):
  File "/home/lennart/Documents/Test/Test.py", line 8, in <module>
    liste=json.loads(line)
  File "/usr/lib/python3.5/json/__init__.py", line 312, in loads
    s.__class__.__name__))
TypeError: the JSON object must be str, not 'bytes'
  • 2
    What is your error? Why do you not load the response as a single json object? – roganjosh Sep 30 '17 at 12:08
  • Ho can load it into single json?
    Traceback (most recent call last): File "/home/lennart/Python/Test/Test.py", line 7, in liste=json.loads(line) File "/usr/lib/python3.5/json/__init__.py", line 312, in loads s.__class__.__name__)) TypeError: the JSON object must be str, not 'bytes'
    – Lennart Groß Sep 30 '17 at 12:21
  • Your traceback should be in the question, not as a comment. You can load the whole thing by getting rid of `for line in content:`. You're iterating the response and then converting to json. – roganjosh Sep 30 '17 at 12:40
  • without the iterating of the response the variable "content" just contains "" maybe I am on complete wrong way. – Lennart Groß Sep 30 '17 at 12:52
  • I would suggest first of all to move to the `requests` library. It is much easier to use, and just call `.json()` on the response. – roganjosh Sep 30 '17 at 12:54
  • Thanks you @roganjosh . – Lennart Groß Sep 30 '17 at 13:16
  • Did that fix your issue? – roganjosh Sep 30 '17 at 13:17
  • Yes. I am happy. "data =json.loads(content.read().decode())" fix it – Lennart Groß Sep 30 '17 at 13:35
  • I suggest you post that as an answer to your own question which will show that the question is resolved. – roganjosh Sep 30 '17 at 13:38

1 Answers1

0

Thanks to roganjosh.
This fixed my issue:

import urllib.request
import json

url= 'https://wex.nz/api/3/info'

content = urllib.request.urlopen(url)

data =json.loads(content.read().decode())
print(data)