As a response to a request I have the following output:
{
[
"1545904980", //val1
"0.058", //val2
"0.049", //val3
"0.058", //val4
"0.049", //val5
"0.018", //val6
"0.000945" //val7
],
[
"1545904920",
"0.058",
"0.072",
"0.072",
"0.058",
"0.103",
"0.006986"
]
}
Let's say I want to access val3 and thus ""0.049", this is what I use:
test = requests.get("url")
# to load response into json we must convert response to string
test_list = str(test.json())
#before loading response into json we must convert '' string to "" type string.
s_test = test_list.replace("\'", "\"")
test_data = json.loads(s_test)
for i in test_data:
for j in i:
print(i[2])
pass
EDIT:
Every time I run this code it just runs but without error. I think it directly moves to "pass". Everything works fine if I just use
for i in test_data:
print(i)
But that prints the whole JSON object which is not the goal. Any help on how to access those specific values?
You are close. I think you are looking for a blend of your two for
loop attempts.
import json
s_test = '''
[
["1545904980", "0.058", "0.049", "0.058", "0.049", "0.018", "0.000945"],
["1545904920", "0.058", "0.072", "0.072", "0.058", "0.103", "0.006986"]
]
'''
test_data = json.loads(s_test)
for row in test_data:
print(row[2])
Should give you:
0.049
0.072