Last active
May 20, 2020 01:56
-
-
Save jamshedazhar/d4cfd6ea3bf0b04658695211636808f3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
flatten_list=lambda l: sum(map(flatten_list,l),[]) if isinstance(l,list) else [l] | |
def flatten_json(json_r): | |
normal = {x: y for x,y in json_r.items() if not isinstance(y, dict)} | |
dict_value = [(x, y) for x,y in json_r.items() if isinstance(y, dict)] | |
if bool(dict_value): | |
new_dict = {".".join([str(value[0]), str(x)]): value[1][x] for value in dict_value for x in value[1]} | |
normal.update(new_dict) | |
return flatten_json(normal) | |
else: | |
list_value = [(x, y) for x,y in json_r.items() if isinstance(y, list) and bool(y)] | |
if bool(list_value): | |
x = [[(v[0], x) for x in v[1]] for v in list_value ] | |
normal_items = list(normal.items()) | |
normal = [dict(normal_items+list(z)) for z in itertools.product(*x)] | |
normal = flatten_list([ flatten_json(x) for x in normal]) | |
return normal |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment