There is a json like this:
{
\"P1\": \"ss\",
\"Id\": 1234,
\"P2\": {
\"P1\": \"cccc\"
},
\"P3\": [
{
\"P1\": \"aaa\"
}
Using json
to convert the json to Python objects and then going through recursively works best. This example does include going through lists.
import json
def get_all(myjson, key):
if type(myjson) == str:
myjson = json.loads(myjson)
if type(myjson) is dict:
for jsonkey in myjson:
if type(myjson[jsonkey]) in (list, dict):
get_all(myjson[jsonkey], key)
elif jsonkey == key:
print myjson[jsonkey]
elif type(myjson) is list:
for item in myjson:
if type(item) in (list, dict):
get_all(item, key)