2

I have below Json.

My requirement is to search this Json and get 'id' value if 'name' is equal to 'Latisha Chase'

[
  {
    "_id": "5d3121cd001453772160a791",
    "friends": [
      {
        "id": 6,
        "name": "Mcknight Tran"
      },
      {
        "id": 7,
        "name": "Helena Bowers"
      },
      {
        "id": 8,
        "name": "Dorsey Ayala"
      }
    ]
    },
  {
"_id": "5d3121cd838efa513e7dda96",
"friends": [ {
  "friends": [
  {
    "id": 90,
    "name": "w Stark"
  },
  {
    "id": 91,
    "name": "w Jacobs"
  },
  {
    "id": 93,
    "name": "w Garner"
  }
]},
  {
    "id": 10,
    "name": "Amalia Stark"
  },
  {
    "id": 11,
    "name": "Myra Jacobs"
  },
  {
    "id": 12,
    "name": "Norton Garner"
  }
]
}

]

This is sample code that I have. Could anyone help me with this.?

I tried recursive codes online but didn't work with my example here.

Update: Its not necessary that 'friends' will have single depth. it can have friends inside friends. ex: friends [{ friends[ {}]}]

2 Answers 2

2

A more general approach using recursion:

def recursive_function(name, l):
    if isinstance(l,list):
        for i in l:
            recursive_function(name, i)
    elif isinstance(l,dict):
        if l.get("name") == name:
            print (l.get("id"))
        for v in l.values():
            if isinstance(v, list) or isinstance(v, dict):
                recursive_function(name, v)

recursive_function("Latisha Chase",json_obj)

Result:

3
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the reply. It didn't work when more than one depth exists. " "friends": [ { "friends": [ { "id": 90, "name": "w Stark" }, { "id": 91, "name": "w Jacobs" }, { "id": 93, "name": "w Garner" } ]}]
Can you update in your code in the question? It's hard to see.
done. I cant paste my original code. so I pasted example code. thanks!
Pretty sure it works just the same. You didn't format the data into a valid json though so make sure the format is correct before running the function.
Great. Thank much!
|
2

Try this

j = [{
    "_id": "5d3121cd001453772160a791",
    "friends": [{
        "id": 6,
        "name": "Mcknight Tran"
      },
      {
        "id": 7,
        "name": "Helena Bowers"
      },
      {
        "id": 8,
        "name": "Dorsey Ayala"
      }
    ]
  },
  {
    "_id": "5d3121cded44d8ba6ad96b78",
    "friends": [{
        "id": 2,
        "name": "June Gilbert"
      },
      {
        "id": 3,
        "name": "Latisha Chase"
      },
      {
        "id": 4,
        "name": "Franco Carlson"
      }
    ]
  },
  {
    "_id": "5d3121cd838efa513e7dda96",
    "friends": [{
        "id": 10,
        "name": "Amalia Stark"
      },
      {
        "id": 11,
        "name": "Myra Jacobs"
      },
      {
        "id": 12,
        "name": "Norton Garner"
      }
    ]
  }
]
for x in j:
  for y in x.get('friends'):
    if y.get('name') == 'Latisha Chase':
      print y.get('id')

3 Comments

Thank you for the response. Its not necessary that Json will have constant depth. it can have multiple 'friends' dicts inside. for ex: friends [{ friends [{ friends {}[}]}]]
Then you have iterate with the keys friends as you want. I am answering it base on your question input.
I think i have updated the question now. Could you update the code above. Sorry Im still learning dictionaries in python

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.