How do I return an upper field in a JSON with python?

54 Views Asked by At

So, I need some help returning an ID having found a certain string. My JSON looks something like this:

{
"id": "id1"
"field1": {
 "subfield1": {
  "subrield2": {
   "subfield3": {
    "subfield4": [
     "string1",
     "string2",
     "string3"
     ]
    }
   }
  }
 }
"id": "id2"
"field1": {
 "subfield1": {
  "subrield2": {
   "subfield3": {
    "subfield4": [
     "string4",
     "string5",
     "string6"
     ]
    }
   }
  }
 }
}

Now, I need to get the ID from a certain string, for example:

For "string5" I need to return "id2"

For "string2" I need to return "id1"

In order to find these strings I have used objectpath python module like this: json_Tree.execute('$..subfield4'))

After doing an analysis on a huge amount of strings, I need to return the ones that are meeting my criterias. I have the strings that I need (for example "string3"), but now I have to return the IDs.

Thank you!!

Note: I don't have a lot of experience with coding, I just started a few months ago to work on a project in Python and I have been stuck on this for a while

1

There are 1 best solutions below

0
On

Making some assumptions about the actual structure of the data as being:

[
  {
    "id": "id1",
    "subfield1": {
      "subfield2": {
        "subfield3": {
          "subfield4": [
            "string1",
            "string2",
            "string3"
          ]
        }
      }
    }
  }
  // And so on
]

And assuming that each string1, string2 etc. is in only one id, then you can construct this mapping like so:

data: List[dict] # The json parsed as a list of dicts

string_to_id_mapping = {}

for record in data:
    for string in record["subfield1"]["subfield2"]["subfield3"]["subfield4"]:
        string_to_id_mapping[string] = record["id"]

assert string_to_id_mapping["string3"] == "id1"

If each string can appear in multiple ids then the following will catch all of them:

from collections import defaultdict

data: List[dict] # The json parsed as a list of dicts

string_to_id_mapping = defaultdict(set)

for record in data:
    for string in record["subfield1"]["subfield2"]["subfield3"]["subfield4"]:
        string_to_id_mapping[string].add(record["id"])

assert string_to_id_mapping["string3"] == {"id1"}