Interview Query

Flatten JSON

Start Timer

0:00:00

Upvote
15
Downvote
Save question
Mark as completed
View comments (14)
Next question

Given a JSON string 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.

.
.
.
.
.


Comments

Loading comments