0

Suppose I have function that traverses through a python data structure and returns all the paths of the data Im looking to parse in a list format:

['section', 'section', 'section', 1, 'name', ]
['section', 'section', 'section', 1]
['section', 'section']

I then have another function that iterates through the same json to parse the data

with open(json_file) as data_file:
json_202 = data.load(data_file)



def parseJson(*argv) :
    for arg in argv:
       #do stuff

section1 = json_202["section"]["section"]["section"][1]["name"]
section2 = json_202["section"]["section"]["section"][1]
section3 = json_202["section"]

I call this function like so:

parseJson(section1, section2, section3)

What is a more pythonic approach of dynamically converting the list results from the first function to a format that matches 2nd function instead of hard cording section1, section2, section3

Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59
Darth
  • 219
  • 2
  • 6
  • 18
  • Will first element of list always be the key of json objects – Arpit Solanki Jun 17 '17 at 21:03
  • So, what do you mean by "json", because it looks like you are working with python lists/dicts, not JSON, which is a text-based serialization format. – juanpa.arrivillaga Jun 17 '17 at 21:06
  • Yes all the elements in list are keys to the data Im looking to parse – Darth Jun 17 '17 at 21:06
  • No, I mean, what is the variable `json`. – juanpa.arrivillaga Jun 17 '17 at 21:07
  • The point I'm trying to get at is that JSON is a text-based serialization format. If you are working with Python data structures, then you no longer are working with "json", because you've already *deserialized* the JSON file. – juanpa.arrivillaga Jun 17 '17 at 21:08
  • @ juanpa.arrillaga I did not include the full code but basically 'json' is ponting the json file that i loaded using import json library – Darth Jun 17 '17 at 21:08
  • Then this: `json["section"]["section"]["section"][1]["name"]` makes no sense. A file-handler isn't indexable. Certainly not with strings. So again, I ask, did you *deserialize the JSON file*, and now, `json` is actually some sort of nested dict/list? – juanpa.arrivillaga Jun 17 '17 at 21:09
  • Apologies actually its a typo, my code is like: `with open(json_file) as data_file:` `json_202 = json.load(data_file)` So "json" should actually be "json_202" – Darth Jun 17 '17 at 21:13
  • 1
    @Darth OK, that is what I'm getting at. `json_202` is **not a json**. It is some Python data structure. This question has nothing to do with json, but with Python lists/dicts. – juanpa.arrivillaga Jun 17 '17 at 21:16
  • So, it is incorrect to say that "I have function that traverses through a json file", and it's making your question confusing. – juanpa.arrivillaga Jun 17 '17 at 21:16
  • @ juanpa.arrillaga, True, I have made the appropriate edits. Thanks – Darth Jun 17 '17 at 21:19
  • Are you looking for [this functionality](https://stackoverflow.com/questions/14692690/access-nested-dictionary-items-via-a-list-of-keys)? I am quite confused as to what you are asking for. – Tadhg McDonald-Jensen Jun 17 '17 at 21:26
  • @TadhgMcDonald-Jensen, thanks for pointer, reduce function has certainly been helpful in creating my custom function – Darth Jun 18 '17 at 20:53

1 Answers1

0

The code below shall perform the same, but in other way:

def parse_json_by_path(pathes, data):
    for path in pathes:
        item = data
        for key in path:
            item = item.get(key)
        # do stuff


    parse_json_by_path([["section", "section","section", 1, "name",], 
                        ['section', 'section', 'section', 1],
                        ['section', 'section']], json_202)
Andriy Ivaneyko
  • 20,639
  • 6
  • 60
  • 82
  • Thank you, any idea why im getting "AttributeError: 'list' object has no attribute 'get'", when i attempt to print item? – Darth Jun 17 '17 at 21:52