Flattening a JSON Object Using Recursion in Python

Поділитися
Вставка
  • Опубліковано 14 лип 2024
  • As part of our back-to-basic series Liz breaks down a real coding interview challenge she completed which landed her a job. This week’s problem uses recursion to flatten a JSON object in python.
    The Challenge:
    Write a function to flatten a JSON object with nested keys into a single level
    Video Overview:
    0:59 The problem
    1:45 Example walkthrough
    3:10 Coding a solution
    18:30 Walking through with a debugger
    23:03 Closing
    Please subscribe to our channel!
    If you have any recommendations for videos you’d like to see, please comment below.
    Additional Resources:
    * Practice hundreds of real coding challenges at coderbyte.com/
    * Need more practice? Check out our channel for more videos on preparing for a coding interview / coderbytedevelopers
  • Наука та технологія

КОМЕНТАРІ • 17

  • @samanthalee6749
    @samanthalee6749 2 роки тому +4

    This is the clearest coding tutorial I've ever seen in my life. Liz you are awesome. Very high quality step by step break down of the problem and solution.

  • @HakeemLawrence
    @HakeemLawrence Рік тому

    Liz, I love you for this. You saved my life. I will watch all of your other videos. Thank you so much! I really appreciate it!

  • @christophercarr4207
    @christophercarr4207 2 роки тому +1

    Thank you! I REALLY needed this.

  • @kjuarez29
    @kjuarez29 Рік тому

    One helluva good explanation! Please keep making videos

  • @harshadnaidu4294
    @harshadnaidu4294 2 роки тому +1

    Too good so well explained

  • @todorowael
    @todorowael 10 місяців тому

    Thank you for the clear explanation!

  • @thisoldproperty
    @thisoldproperty Рік тому

    Very clear. Very concise. First time I've come across your videos. How you have more of these. Thank you.

  • @shhhhhhKoiHai
    @shhhhhhKoiHai Рік тому

    Awesome 👍

  • @mayyiaung977
    @mayyiaung977 8 місяців тому

    thank you

  • @gboyegaabidoye6524
    @gboyegaabidoye6524 Рік тому

    great explanation, thank you. what if you want the output in a tabular form

  • @idanqwe1
    @idanqwe1 2 роки тому +2

    Hey, what if we want to get back from flatten to json object?

  • @Troglodyte2021
    @Troglodyte2021 Рік тому

    That jacket of yours, that logo is cool! :)

  • @m.e.k1741
    @m.e.k1741 4 місяці тому +1

    Here is the code that will work on all data structures
    def run_on_dict(data):
    for key, value in data.items():
    if isinstance(value, dict):
    run_on_dict(value)
    elif isinstance(value, list):
    for element in value:
    if isinstance(element, (dict, list)):
    run_on_dict(element)
    else:
    print(f"{key}: {element}")
    else:
    print(f"{key}: {value}")

  • @rahimskmd5314
    @rahimskmd5314 Рік тому

    How do I take one key value pair based on some condition , like from ur example I want city in Canada that starts with M

  • @philtoa334
    @philtoa334 Рік тому

    Nice.

  • @riz699
    @riz699 Рік тому

    where is the code? Please provide the link so that it can be test.

  • @madmonk0
    @madmonk0 2 роки тому

    Nice tutorial, thanks!
    Have you got any tips if I want to only output specific keys in the json object? (Parameterised as a list)
    Eg json object:
    obj = {'key1' :
    {'key2':
    {'key3': 'value1',
    'key4': 'value2'
    }
    }
    }
    When caling the flatten function:
    flatten_json( obj, ["key1.key2", "key3.key4"])
    Desired output:
    {
    "key1.key2": "value1",
    "key3.key5": "value3"
    }