The input JSON as your complete posted is not valied
Tips use jsoncrack to check if valid and see structure.
Tips use jsoncrack to check if valid and see structure.
state.json
Output:{
"states": [
{
"state": [
{
"id": [
"0"
],
"tempValue": [
"2000"
],
"dhw": [
"1"
]
},
{
"id": [
"1"
],
"tempValue": [
"1900"
],
"dhw": [
"1"
]
},
{
"id": [
"2"
],
"tempValue": [
"1700"
],
"dhw": [
"1"
]
},
{
"id": [
"3"
],
"tempValue": [
"1800"
],
"dhw": [
"1"
]
},
{
"id": [
"4"
],
"tempValue": [
"1000"
],
"dhw": [
"1"
]
}
]
}
]
}
Code of your code and print it,insted of the log stuff.import json with open('state.json') as file: Response = json.load(file) if 'states' in Response: for state in Response["states"][0]["state"]: print("id ="+ state["id"][0] + " Temp =" + state["tempValue"][0])
Output:id =0 Temp =2000
id =1 Temp =1900
id =2 Temp =1700
id =3 Temp =1800
id =4 Temp =1000
With f-string
.import json with open('state.json') as file: Response = json.load(file) if 'states' in Response: for state in Response["states"][0]["state"]: print(f'id = {state["id"][0]} Temp = {state["tempValue"][0]}')
Output:id = 0 Temp = 2000
id = 1 Temp = 1900
id = 2 Temp = 1700
id = 3 Temp = 1800
id = 4 Temp = 1000