0

I have the following .json file:

{
    "level1_one":"1",
    "level1_two":{
        "level2_one":"2",
        "level2_two":{
            "level3_one":"bottom"
        }
    }
}

I need to give it nested and flatten representation like so:

{
    "level1_two": {
        "level2_two": {
            "level3_one": "bottom"
        },
        "level2_one": "2"
    },
    "level1_one": "1"
}

and

{
    "level2_one": "2",
    "level3_one": "bottom",
    "level1_one": "1"
}

I know I can easily achieve the nested form by simply doing this:

def json_parser(filename):
    data = json.load(open(filename))
    print(data)

The problem is it needs to be done by using recursion for both cases. I tried almost everything I could find around here but without much success. How I can do this?

4
  • 1
    What did you try? And why you need to do it with recursion? Commented Dec 5, 2017 at 16:52
  • Almost everything I could find by searching recursion. The problem is once getting to the bottom of the json I can't find a way to represent it back. It needs to be done this way as it's kind of challenge. I have some experience with JSON but this is coming a bit wired. Commented Dec 5, 2017 at 17:02
  • So are you saying it doesn't really have to be recursive? Commented Dec 5, 2017 at 17:37
  • thanks to @Ajax1234 the flatten pattern is done. About the nested I already know how to do it without it but it needs to be done by using recursion. Commented Dec 5, 2017 at 17:41

2 Answers 2

1

You can try this:

s = {
"level1_one":"1",
"level1_two":{
    "level2_one":"2",
    "level2_two":{
        "level3_one":"bottom"
    }
  }
}
def flatten(s):   
   for i in s:
      if not isinstance(s[i], dict):
          yield (i, s[i])
      else:
           for b in flatten(s[i]):
              yield b

new_data = dict(list(flatten(s)))

Output:

{'level2_one': '2', 'level3_one': 'bottom', 'level1_one': '1'}
Sign up to request clarification or add additional context in comments.

3 Comments

Great. As expected. Can you suggest something for the nested version.
@sretko glad to help! Regarding the nested version, I do not seen any difference between your desired nested version and the original data structure? Could you clarify this?
It's just edited. Take another look and you are going to see it clearly.
0

Something like this

def flatten_dict(d):
    def items():
        for key, value in d.items():
            if isinstance(value, dict):
               for subkey, subvalue in flatten_dict(value).items():
                    yield subkey, subvalue
            else:
                yield key, value

    return dict(items())

this returns

{'level1_one': '1', 'level2_one': '2', 'level3_one': 'bottom'}

for the example you posted

Comments

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.