I made a function that turns all child from a JSON file to parents (or brothers?). It adds the parent name to the key, so there are no duplicates.
It works ok, but I would like to ask if there are better ways to do this, in a logic, pythonic and even syntaxic way. I feel I am stuck with if-else logic.
Thank you!
I have this JSON file:
{
"update_id":1,
"message":{
"message_id":1,
"from":{
"id":1,
"is_bot":false,
"first_name":"Albert",
"language_code":"es"
},
"chat":{
"id":1,
"first_name":"Albert",
"type":"private"
},
"date":1,
"text":"HI"
}
}
Filtering and convertion from child to parents is done this code:
MF_FIELDS = (
"message_from_id",
"message_from_is_bot",
"message_from_first_name",
"message_from_username",
"message_chat_id",
"message_chat_title",
"message_chat_type",
"message_date",
"message_text",
"message_entities_type"
)
def dict_to_first_level(d, parent_name=None, rec_dict=None):
ret_dict = {}
if rec_dict: ret_dict = {**ret_dict, **rec_dict}
for k, v in d.items():
if isinstance(v, dict):
if parent_name: k = f'{parent_name}_{k}'
_recursive = dict_to_first_level(v, k, ret_dict)
ret_dict = {**ret_dict, **_recursive[0]}
else:
if parent_name:
k = f'{parent_name}_{k}'
ret_dict[k] = v
return ret_dict, parent_name, rec_dict
def filter_json(encoded_json, filter):
ret_dict = {}
json_dict = json.loads(encoded_json)
first_level_dict, _, _ = dict_to_first_level(json_dict)
for k, v in first_level_dict.items():
if k in filter:
ret_dict[k] = v
return ret_dict
Producing this result:
{
"message_from_id":1,
"message_from_is_bot":false,
"message_from_first_name":"Albert",
"message_chat_id":1,
"message_chat_type":"private",
"message_date":1,
"message_text":"HI"
}