0

i have a Json Data that i can access from my own api

But the Json Data is not in a .json file its in Php file (like in the link below)

now i want to use Python to print the Data

import json
from urllib.request import urlopen
with urlopen("https://**********.000webhostapp.com/api/value/read_all.php") as response:
    source = response.read()
data = source
for item in data['value']['temp']:
    print(item)

This the python Script i use

this is the main Error :

for item in data['value']['temp']:
TypeError: byte indices must be integers or slices, not str

The JSON looks like:

{"value":[{"id":"1","temp":"25.60","water":"80%","total":"5L","percent":"50%"}...
5
  • response.read() will return a bytes object, you need to convert it to a string and then use the json library to convert it to a dictionary. Commented Sep 17, 2018 at 20:10
  • i still new in fact can you help me with a code please Commented Sep 17, 2018 at 20:12
  • 1
    You haven't actually used the json library in your code Commented Sep 17, 2018 at 20:15
  • I don't get what relation to PHP is here. Can you reduce the problem? Or can you at least extend the question with a minimal reproducible example? Commented Sep 17, 2018 at 20:17
  • @roganjosh in fact thats right but i use it and still get the same error for item in data['value']['temp']: TypeError: list indices must be integers or slices, not str Commented Sep 17, 2018 at 20:28

2 Answers 2

1

You need to use json.loads() to convert a JSON string to a Python dictionary or list. Use the decode() method to convert the bytes to a string.

data = json.loads(source.decode('utf-8'))

You're also accessing the JSON incorrectly. data['value'] is a list of dictionaries, not a dictionary by itself. The loop should be:

for value in data['value']:
    print(value['temp'])
Sign up to request clarification or add additional context in comments.

11 Comments

still get the same error : for item in data['value']['temp']: TypeError: list indices must be integers or slices, not str
That's not the same error. It says "list indices", not "byte indices". That means that the JSON contains an array, not an object. You need to post a sample of the JSON so we can see how to properly access it.
In JSON, [] creates a list, you need to use numeric indicecs access its elements, {} creates a dictionary, it has string indices.
ok this is a simple : {"value":[{"id":"1","temp":"25.60","water":"80%","total":"5L","percent":"50%"}
for data['value'] as value: print(value['temp'])
|
0

You are using urlopen to read data from a webpage which will return a response object. You can call response.read() which will return a byte-string. This is simply the sequence of bytes your website has sent.

Since you are assuming those bytes are valid JSON, you will be able to decode them to a string which you can do with the bytes.decode method. Assuming you're using the UTF-8 charset this would be bytes.decode('utf-8')

To load a JSON formatted string as a dictionary, you can use the built-in json module, which I see you have imported at the top of your code.

Together this will look like:

import json
from urllib.request import urlopen
with urlopen("https://**********.000webhostapp.com/api/value/read_all.php") as response:
    source = response.read()
    my_string = source.decode('utf-8')
    my_dictionary = json.loads(my_string)

for item in my_dictionary['value']['temp']:
    print(item)

1 Comment

Not working its the same error : for item in my_dictionary['value']['temp']: TypeError: list indices must be integers or slices, not str

Your Answer

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.