0

I have very specific problem. This is the json file I have with me.

    {
      "name" : "myname",
      "value" : 0,
      "level" : [ {
      "name" : "myname",
      "value" : 1,
      "level" : [ {
            "name" : "something",
            "value" : 2,
            "level" : [ {
            "name" : "something",
            "value" : 3,
                "level" : [ {
                    "name" : "Phy",
                    "value" : 4,
                    "something_retrive" : [ {
                       "mysupername" : "retrive",
                       "someID" : "blahblah"
             }, {
                       "something_retrive" : [ {
                       "mysupername" : "retrive",
                       "someID" : "blahblah"
                       "randomdata" : [ {
                       "data1":"val1"
                       "data1":"val1"   

             }, {
                       "name" : "some",
                       "size" : 3,
                       "type" : "float",
             } ]
          } ]
       } ]
     } ]
   } ]
  } ]
}

I need to search through level inside level(Which can be of any numbers recursively) and I also need to know no. of them until I find "something_retrive" and in that also there can be more than one "mysupername" so I need to retrive some of them after knowing no. of "mysupername" available.

I'm using python right now and using them I'm able to load json and access the data using answers from this question Parsing json and searching through it

But I don't know How to perform searching and brwosing in JSON file and how to know that I'm at specific level.

Any help or suggestion or reference will be helpful.

(I searched for 2 days but no luck)

Community
  • 1
  • 1
niyant
  • 389
  • 1
  • 3
  • 12

1 Answers1

0

Note that dictionaries are, by definition, unordered, so you might need to force the JSON decoder to use an ordereddict. Once you have your data, though, it is fairly easy to loop through and build data structures that give you a tuple of keys necessary to access any given data. Here is an example:

mydict =   {
            "name" : "myname",
            "value" : 0,
            "level" : [ {
                "name" : "myname",
                "value" : 1,
                "level" : [ {
                    "name" : "something",
                    "value" : 2,
                    "level" : [ {
                        "name" : "something",
                        "value" : 3,
                        "level" : [ {
                            "name" : "Phy",
                            "value" : 4,
                            "something_retrive" : [
                                {
                                    "mysupername" : "retrive",
                                    "someID" : "blahblah"
                                }, {
                                    "something_retrive" : [ {
                                        "mysupername" : "retrive",
                                        "someID" : "blahblah",
                                        "randomdata" : [
                                            {
                                                "data1":"val1"
                                            }, {
                                                "name" : "some",
                                                "size" : 3,
                                                "type" : "float",
                                            }
                                        ]
                                    } ]
                                }
                            ]
                        } ]
                    } ]
                } ]
            }]
        }

import collections

def findpaths(container):
    def recurse(container, prefix):
        if isinstance(container, list):
            container = enumerate(container)
        else:
            container = container.items()
        for key, value in container:
            keys[key].append(prefix)
            fullkey = prefix + (key,)
            if isinstance(value, (list, dict)):
                recurse(value, fullkey)
            else:
                values[value].append(fullkey)
    keys = collections.defaultdict(list)
    values = collections.defaultdict(list)
    recurse(container, ())
    return keys, values

def retrievebypath(container, keys):
    for key in keys:
        container = container[key]
    return container

keys, values = findpaths(mydict)

print("\nBy key:\n")
for key, value in sorted(keys.items(), key=str):
    print(key, sorted(value))

print("\nBy value:\n")
for key, value in sorted(values.items(), key=str):
    print(key, sorted(value))

print("\nRetrieving container which has second appearance of mysupername\n")
print(retrievebypath(mydict, keys["mysupername"][1]))
Community
  • 1
  • 1
Patrick Maupin
  • 8,024
  • 2
  • 23
  • 42
  • Superbly Done ...!!! Thanks alot ... I built yesterday only my own solution but this one is also awesome .. just ran it .. with few tweaks.. What I did is loaded with json lib and then using the list datastructure accessed using it. lot of while loops but works like charm – niyant Aug 21 '15 at 05:18