i have json file, when loaded in python using json.loads()
becomes dictionary
. json data nested dictionary
can contain 'groups'
key inside 'groups'
key. values inside 'groups'
key 'name'
key , 'properties'
key.
each 'properties'
key has unique 'name'
, 'value'
key.
my objective search 'groups'
key having 'name'
key value "sportcar"
, has properties
key having name
key value "bmw"
, , when these conditions satisfied, update 'data'
key 'data':value1
'data':value2
.
an example of json follows
{ "groups": [ { "name": "sportcar", "properties": [ { "name": "bmw", "value": { "type": "string", "encoding": "utf-8", "data": "value1" } }, { "name": "audi", "value": { "type": "boolean", "data": true } } ], "groups": [ { "name": "trucks", "properties": [ { "name": "volvo", "value": { "type": "string", "encoding": "utf-8", "data": "value1" } } ] } ] }, { "name": "motorcycle", "properties": [ { "name": "yamaha", "value": { "type": "string", "encoding": "utf-8", "data": "value1" } } ], "groups": [ { "name": "speeders", "properties": [ { "name": "prop2", "value": { "type": "string", "encoding": "utf-8", "data": "value1" } } ] } ] } ] }
the above json contained in myjson22.json. here have tried far:
import json pprint import pprint json_data=open('myjson22.json', 'r') data = json.load(json_data) #print(data) def get_recursively(search_dict, field): """ read json data type dict , search 'groups' keys 'name' key value value provided. """ fields_found = [] key, value in search_dict.items(): if key == field: fields_found.append(value) elif isinstance(value, dict): results = get_recursively(value, field) result in results: fields_found.append(result) elif isinstance(value, list): item in value: if isinstance(item, dict): more_results = get_recursively(item, field) another_result in more_results: fields_found.append(another_result) return fields_found get_recursively(data, ["properties"][0])
and output was:
[[{'name': 'bmw', 'value': {'data': 'value1', 'encoding': 'utf-8', 'type': 'string'}}, {'name': 'audi', 'value': {'data': true, 'type': 'boolean'}}], [{'name': 'volvo', 'value': {'data': 'value1', 'encoding': 'utf-8', 'type': 'string'}}], [{'name': 'yamaha', 'value': {'data': 'value1', 'encoding': 'utf-8', 'type': 'string'}}], [{'name': 'prop2', 'value': {'data': 'value1', 'encoding': 'utf-8', 'type': 'string'}}]]
a way implement recursive solution backtracking. when no more 'groups'
keys are found nested inside root key, 'name'
key value matched groups_name
parameter, 'sportcar' in our case. if condition satisfied check values inside same 'groups'
key's (i.e. 'sportcar' key's) 'properties'
key , match 'name'
key value properties_name
parameter (which 'bmw' in our case). if second condition true, update 'data'
key value inside same 'properties'
key, per requirements, or else return (for backtracking).
import json json_data = open('myjson22.json', 'r') data = json.load(json_data) def get_recursively( myjson, groups_name, properties_name, value2): if 'groups' in myjson.keys(): # there multiple values inside 'groups' key jsoninsidegroupskey in myjson['groups']: get_recursively( jsoninsidegroupskey, groups_name, properties_name, value2) if 'name' in myjson.keys(): # check groups name if myjson['name'] == groups_name: # check properties name if myjson['properties'][0]['name'] == properties_name: # update value. changes persist backtrack because # making update @ original memory location # , not on copy. more info see deep , shallow copy. myjson['properties'][0]['value']['data'] = value2 return get_recursively(data,'sportcar','bmw','changedvalue1') get_recursively(data,'speeders','prop2','changedvalue2') print data
my output:
{u'groups': [{u'name': u'sportcar', u'groups': [{u'name': u'trucks', u'properties': [{u'name': u'volvo', u'value': {u'data': u'value1', u'type': u'string', u'encoding': u'utf-8'}}]}], u'properties': [{u'name': u'bmw', u'value': {u'data': 'changedvalue1'
, u'type': u'string', u'encoding': u'utf-8'}}, {u'name': u'audi', u'value': {u'data': true, u'type': u'boolean'}}]}, {u'name': u'motorcycle', u'groups': [{u'name': u'speeders', u'properties': [{u'name': u'prop2', u'value': {u'data': 'changedvalue2'
, u'type': u'string', u'encoding': u'utf-8'}}]}], u'properties': [{u'name': u'yamaha', u'value': {u'data': u'value1', u'type': u'string', u'encoding': u'utf-8'}}]}]}
prettified as:
{ "groups": [ { "name": "sportcar", "properties": [ { "name": "bmw", "value": { "type": "string", "encoding": "utf-8", "data": "changedvalue1" } }, { "name": "audi", "value": { "type": "boolean", "data": true } } ], "groups": [ { "name": "trucks", "properties": [ { "name": "volvo", "value": { "type": "string", "encoding": "utf-8", "data": "value1" } } ] } ] }, { "name": "motorcycle", "properties": [ { "name": "yamaha", "value": { "type": "string", "encoding": "utf-8", "data": "value1" } } ], "groups": [ { "name": "speeders", "properties": [ { "name": "prop2", "value": { "type": "string", "encoding": "utf-8", "data": "changedvalue2" } } ] } ] } ] }
Comments
Post a Comment