Firsly, the property names (in this case 1, 2, 3) should be enclosed in double quotes such as "1", "2" and "3"within your JSON data. Then use json.loads function along with a cursor containing an if condition to extract the desired value such as
import json
data_set = '{"1": [1, {"name":"bob"}, 3], "2": [1, {"name":"bill"}, 3],"3": [1, {"name":"fred"}, 3]}'
loaded_json = json.loads(data_set)
for i in loaded_json:
if int(i) == 1:
print( loaded_json[i] )
Agreed. Expanding on your code I am added an extra element to the JSON. I wanted to put the [1]['name'] and [3]['city'] into a string list so that I can loop through the list extracting data. The JSON will be much more complex and I will be extracting a large set of variables.
data_set = '{"1": [1, {"name":"bob"}, 3,{"city":"London"}], "2": [1, {"name":"bill"}, 3,{"city":"Madrid"}],"3": [1, {"name":"fred"}, 3,{"city":"Portland"}]}'
loaded_json = json.loads(data_set)
for i in loaded_json:
print(loaded_json[i][1]['name'])
print(loaded_json[i][3]['city'])
"[1]['name']"which you want to use to traverse the dictionary / list in depth? Where is this string coming from?[1, 'name']and then iterate over it.