2

I want to be able to find all occurrences of a particular json object in a json file using one of the keys that it has. For instance if I have the following json block :

{
                  "FNMIP": 1,
                  "FNMIT": 0,
                  "FNMP": {
                    "DP": {
                      "DT": 0,
                      "UF": true,
                      "DF": "yyyyMMdd"
                    },
                    "FP": {
                      "FE": null
                    }
                  },
                  "IE": true,
                  "$": "PMF"
                }

I want to search for all the json block that contain this key and then apply some kind a logic to replace it with another json object. I am not able to figure out as to how to do that stuff in Python as I am new to Python.

AnkitSablok
  • 3,021
  • 7
  • 35
  • 52
  • So, you want to do a DFS search, and when you find a match, append that object to an array? You will just need to recursively walk it – Fallenreaper Apr 07 '16 at 19:14
  • I get the idea of doing depth first search, what I want to do is do a DFS and when I find that object replace it with a new object and continue my search to look for more objects of that type. – AnkitSablok Apr 07 '16 at 19:15
  • could you provide an example data and modification? You might want to look into [the documentation on `dict` object type](https://docs.python.org/2/library/stdtypes.html#mapping-types-dict) – Tadhg McDonald-Jensen Apr 07 '16 at 19:48

1 Answers1

2

If I understand correctly you want to recursively walk nested dicts and for any value that matches a particular value replace it with something else. so do this:

def recursive_replace(data,match,repl):
    for k,v in data.items():
        if v == match:
            data[k] = repl #replace the matched value
        elif isinstance(v,dict):
            recursive_replace(v,match,repl)

then you can just convert the data back into json.

to instead test if "$" key is present in the data you can use the in operator:

def recursive_replace(data,repl):
    for k,v in data.items():
        if not isinstance(v,dict):
            continue
        elif "$" in v:
            data[k] = repl
        else:
            recursive_replace(v,repl)
Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59
  • so, the thing is when I see that for json object like the one above if "$":"PMF" then replace it with a new object altogether. – AnkitSablok Apr 07 '16 at 19:38
  • _replace it with a new object_ like what? Is it a static value or determined based on where the data is located in the grand scheme of things? do you want data like `{"tadhg":{"$":"A"},"Ankit":{"$":"B"}}` to replace the "$" key with the key from one level up in the structure? So that the new data is like `{"tadhg":{"tadhg":"A"},"Ankit":{"Ankit":"B"}}` – Tadhg McDonald-Jensen Apr 07 '16 at 19:50
  • No, so if I find something with "$":"A" then I want to replace the parent object that can be dictionary in which "$" is a key with a new dictionary so if the structure was like {"X": "S", "V": { "$" : PFM}} then replace it with {"P": "A"} for example. – AnkitSablok Apr 07 '16 at 19:56
  • I added to the answer but I'm still unclear if this is what you want. How is the replacement value being defined? If you simply replace all the values with a single dictionary they will all end up being references to the same object and you will get problems like http://stackoverflow.com/questions/7255383/python-list-append-behavior – Tadhg McDonald-Jensen Apr 07 '16 at 20:12
  • @TadhgMcDonald-Jensen this was a nice solution for a similar problem I had. Thank you! – Gibron Mar 30 '23 at 20:38