Interview Query

Flatten JSON

4
Have you seen this question before?

Given a JSON object with nested objects, write a function flatten_json that flattens all the objects to a single key-value dictionary. Do not use the library that actually performs this function.

Example:

Input:

import json
json_str = json.dumps({'a':{'b':'c', 'd':'e'}})

Output:

def flatten_json(json_str) -> json.dumps({'a_b':'c', 'a_d':'e'})

Note: Input and output are in string format : use json.dumps() to convert python dictionary to string.

Next question: Top Three Salaries
.....
Python 3.9.6
Loading editor
Use Shift + Enter to run code