0

I have dynamically changing json file (not the entire file changes dynamically, its just at one point which I will specify below) and I need to iterate using for loop at that point (where it changed dynamically) so that I can grab required elements inside that bracket in json file. Below is json snippet what it looks like.

"A" : {
  "uni/aa/bb" (----> This changes randomly): [ {
    "Name" : "cc",
    "Id" : "1",
    }, {
    "Name" : "cc",
    "Id" : "1",
    } ]
}

I used re.search to match the pattern I get at that point. But no luck. I need to store Name and Id values ultimately. Any suggestions?

resp = json.loads(resp) ---> This gives me above mentioned json output

Here are the sample of codes I am trying.

for k in resp['A']:
    for v in k['uni/aa/bb']: #---> TypeError: string indices must be integers

for k in resp['A']:
    m = re.search('uni/(.*)') #--> Because this is changing dynamically
        if m:
            name = str(m['Name']) #---> TypeError: '_sre.SRE_Match' object has no attribute '__getitem__'
GLR
  • 1,070
  • 1
  • 11
  • 29
chaitanya
  • 11
  • 1
  • 7
  • 1
    It's hard to tell exactly what you're asking. Could you show the code you tried, and explain how it's not working? – glibdud May 22 '17 at 20:29
  • @chaitanya If it the case that you always want to know the string key that belongs to the child of "A" object and don't mind removing the item from json, you can try poping the thing. Like this: `key, value = resp["A"].popitem()`. From this `key`, you can get those `uni/aa/bb` strings, whatever it maybe. And after that you can also traverse that child further down the depth like this: `resp["A"][key]["Name"]`. Reference: https://stackoverflow.com/a/3097896/623810 – PEIN May 22 '17 at 20:38
  • You can also keep all the keys in one list using json.keys() and when the json changes , check which key is not in this list and iterate over that? – iamkhush May 22 '17 at 20:40
  • I ve added two logics to the question that Ive been trying. Any suggestions? – chaitanya May 22 '17 at 20:41
  • @chaitanya Instead of `for v in k['uni/aa/bb']:` use this: `for key, value in k.items():` in case of `python3.x` or `for key, value in k.iteritems():` for `python2.x`. Reference: https://stackoverflow.com/a/3294899/623810 – PEIN May 22 '17 at 20:43
  • @PEIN I am trying to understand your two suggestions. Can you explain bit more detailed any one of your suggestions? – chaitanya May 22 '17 at 20:49
  • @PEIN I tired your resp["A"][key]["Name"] suggestion. But I am getting "TypeError: list indices must be integers, not str" – chaitanya May 22 '17 at 21:04
  • @chaitanya I see. I've published a code where I've explained a bit the first approach. http://ideone.com/02fGiC And the second approach can be found here: http://ideone.com/HCVfkC . Hope this helps. – PEIN May 22 '17 at 21:22
  • @PEIN Thanks:). Ive achieved it with your logic. – chaitanya May 22 '17 at 22:16
  • @chaitanya You are welcome. – PEIN May 23 '17 at 10:19

1 Answers1

0

If it the case that you always want to know the string key that belongs to the child of "A" object and don't mind removing the item from json, you can try poping the thing. Like this: key, value = resp["A"].popitem(). From this key, you can get those uni/aa/bb strings, whatever it maybe. And after that you can also traverse that child further down the depth like this: resp["A"][key][0]["Name"]. Reference.

Sample code.

Another approach could be like the following. Instead of for v in k['uni/aa/bb']: use this: for key, value in k.items(): in case of python3.x or for key, value in k.iteritems(): for python2.x. Reference.

Sample code.

PEIN
  • 308
  • 1
  • 5
  • 17