-3

I want to replace the values inside a json for dictionary's key it could be present as dictionay directly in json or as a dictionary inside another list as below:

{ 
  "appType": "popper",
  "createdAt": "1970-01-01T00:00:00.000Z",
  "updatedAt": "1970-01-01T00:00:00.000Z",
  "people": [{
            "name": "Vol1",
            "label": "Vol1",
            "peopleInfo": [{
                "name": "ram",
                "age": "2407653459860",
                "id": "1b738651-da9f-4c85-88c1-70dbfe1976681"
            }],
            "itemInfo": {
                "id": "ee763970-51e2-57a5-955c-d72fc3e28a3f",
                "name": "xyz",
                "type": "any",
                "managed": False
            }
        }],
  "itemInfo": [{
            "managed": False,
            "vendorName": "any",
            "serialNumber": "AF-10124"
             }],
}

Desired Output:

{ 
  "appType": "popper",
  "createdAt": "1970-01-01T00:00:00.000Z",
  "updatedAt": "1970-01-01T00:00:00.000Z",
  "peopleInfo": [{
            "name": "Vol1",
            "label": "Vol1",
            "people": [{
                "name": "ram",
                "age": "2407653459860",
                "id": "1b738651-da9f-4c85-88c1-70dbfe1976681"
            }],
            "itemInfo": {
                "id": "ee763970-51e2-57a5-955c-d72fc3e28a3f",
                "name": "xyz",
                "type": "any",
                "managed": True
            }
        }],
  "itemInfo": [{
            "managed": True,
            "vendorName": "any",
            "serialNumber": "AF-10124"
             }],
}

so as in desired output i want to update/replace the managed flag as True from false for itemInfo directly in json and as well as itemInfo in peopleInfo List using python. The iteminfo dictionary can be present in entire json in some different list as well. Thankyou for the help.

I have written below code but not able to make it a general one:

i["details"]["storageSystemsInfo"][0]["managed"] = True

KulJeet
  • 3
  • 3

1 Answers1

1

Update 2:

Change only "managed" fields within "itemInfo" dictionary like you asked. (This is not pretty or clean code)

# Python 3
def change_this_key_to_true(key, var):
    if hasattr(var,'items'):
        for k, v in var.items(): 
            if k == key:
                var[k] = True
            if isinstance(v, dict):
                change_this_key_to_true(key, v)
            elif isinstance(v, list):
                for d in v:
                    change_this_key_to_true(key, d)
    else:
        if isinstance(var, list):
            for element in var:
                change_this_key_to_true(key, element)


all_item_info_dics = []
def get_all_values_with_key(key, var):
    if hasattr(var, 'items'):
        for k, v in var.items(): 
            if k == key:
                all_item_info_dics.append(var[k])
            if isinstance(v, dict):
                get_all_values_with_key(key, v)
            elif isinstance(v, list):
                for d in v:
                    get_all_values_with_key(key, d)

get_all_values_with_key("itemInfo", myJson)
print(all_item_info_dics)
print("\n")
change_this_key_to_true("managed", all_item_info_dics)
print(myJson)

Update: (That's my first answer, took me a while, hope it's good :) )

# Python 3
def change_this_key_to_true(key, var):
    if hasattr(var,'items'):
        for k, v in var.items():
            if k == key:
                var[k] = True
            if isinstance(v, dict):
                change_this_key_to_true(key, v)
            elif isinstance(v, list):
                for d in v:
                    change_this_key_to_true(key, d)

change_this_key_to_true("managed", myJson)
print(myJson)

Go through all the fields in a complex dict/list/"weird combination of the two" and looking for a field named "managed" and change it to True. Based my answer on this: Find all occurrences of a key in nested dictionaries and lists

Original answer:

x["people"][0]["itemInfo"]["managed"] = True
x["itemInfo"][0]["managed"] = True
Yam Shargil
  • 445
  • 5
  • 9
  • If it helps, please upvote and mark my answer – Yam Shargil Mar 21 '21 at 09:51
  • This is fine and thanks for your help, but i am looking for a general solution where iteminfo can be present anywhere in json and not in a fixed key, so basically peopleInfo key can be different in the json. – KulJeet Mar 21 '21 at 09:55
  • The updated answer is good, thanks , but i need managed flag for only itemInfo dictionary not all managed flags need to be updated. just the one for itemInfo dict/list however it is. and again the name iteminfo can be any other name as well :) – KulJeet Mar 21 '21 at 11:26