My Source file is having JSON data in the format given below.
{ "data": [
{
"type":"c_boolean",
"_id":"5f7b47019d732301005d2d8a",
"object":"c_step_response",
"created":"2020-10-05T16:17:05.889Z",
"creator":{
"_id":"5f7b3a3d1b97a5010099f683",
"object":"account",
"path":"/accounts/5f7b3a3d1b97a5010099f683"
},
"owner":{
"_id":"5f7b3a3d1b97a5010099f683",
"object":"account",
"path":"/accounts/5f7b3a3d1b97a5010099f683"
},
"access":7,
"accessRoles":["000000000000000000000004","000000000000000000000007","000000000000000000000006"],
"favorite":false,
"c_account":{
"_id":"5f7b3a3d1b97a5010099f683",
"object":"account",
"path":"/accounts/5f7b3a3d1b97a5010099f683"
},
"c_end_date":"2020-10-05T16:16:48.000Z",
"c_group":{
"_id":"5f75f6faf4bb0f0100ae31a4",
"object":"c_group",
"path":"/c_groups/5f75f6faf4bb0f0100ae31a4"
},
"c_public_user":{
"_id":"5f7b39f4209e8901002d1800",
"object":"c_public_user",
"path":"/c_public_users/5f7b39f4209e8901002d1800"
},
"c_site":{
"_id":"5f75f705f4bb0f0100ae3887",
"object":"c_site",
"path":"/c_sites/5f75f705f4bb0f0100ae3887"
},
"c_skipped":false,
"c_start_date":"2020-10-05T16:16:47.000Z",
"c_step":{
"_id":"5f75f710f4bb0f0100ae4169",
"object":"c_step",
"path":"/c_steps/5f75f710f4bb0f0100ae4169"
},
"c_study":{
"_id":"5f75f6ecf4bb0f0100ae2ace",
"object":"c_study",
"path":"/c_studies/5f75f6ecf4bb0f0100ae2ace"
},
"c_task":{
"_id":"5f75f6fcf4bb0f0100ae335f",
"object":"c_task",
"path":"/c_tasks/5f75f6fcf4bb0f0100ae335f"
},
"c_task_response":{
"_id":"5f7b46fe9d732301005d26d5",
"object":"c_task_response",
"path":"/c_task_responses/5f7b46fe9d732301005d26d5"
},
"c_value":true,
"shared":false
},
{
......
},
{
......
}
My expected output should be in the format given below:
[
{
"type":"c_boolean",
"_id":"5f7b47019d732301005d2d8a",
"object":"c_step_response",
"created":"2020-10-05T16:17:05.889Z",
"creator": "5f7b3a3d1b97a5010099f683",
"owner":"5f7b3a3d1b97a5010099f683",
"access":7,
"accessRoles":["000000000000000000000004","000000000000000000000007","000000000000000000000006"],
"favorite":false,
"c_account":"5f7b3a3d1b97a5010099f683",
"c_end_date":"2020-10-05T16:16:48.000Z",
"c_group":"5f75f6faf4bb0f0100ae31a4",
"c_public_user":"5f7b39f4209e8901002d1800",
"c_site":"5f75f705f4bb0f0100ae3887",
"c_skipped":false,
"c_start_date":"2020-10-05T16:16:47.000Z",
"c_step":"5f75f710f4bb0f0100ae4169",
"c_study":"5f75f6ecf4bb0f0100ae2ace",
"c_task":"5f75f6fcf4bb0f0100ae335f",
"c_task_response":"5f7b46fe9d732301005d26d5",
"c_value":true,
"shared":false
}
{
....
....
....
}
{
....
....
....
}
How can I dynamically loop through the the source JSON file and create a new JSON file with the expected output? So, basically here i want to ignore all the object and path which are available under creator, owner, c_account, etc. and for example 'creator' key must be having the value of '_id'. 'creator': '5f7b3a3d1b97a5010099f683'. This I should do it for whole JSON file.
Here is my code which I have done and the output for it:
my_dict ={}
v_type = []
v_id = []
v_object = []
v_created = []
v_creator = []
v_owner = []
v_access = []
v_accessroles = []
v_favorite = []
v_c_account = []
v_c_end_date = []
v_c_group = []
v_c_public_user = []
v_c_site = []
v_c_skipped = []
v_c_start_date = []
v_c_step = []
v_c_study = []
v_c_task = []
v_c_task_response = []
v_c_value = []
v_shared = []
for i in contents['data']:
v_type.append(i['type'])
v_id.append(i['_id'])
v_object.append(i['object'])
v_created.append(i['created'])
v_creator.append(i['creator']['_id'])
v_owner.append(i['owner']['_id'])
v_access.append(i['access'])
v_accessroles.append(i['accessRoles'])
v_favorite.append(i['favorite'])
v_c_account.append(i['c_account']['_id'])
v_c_end_date.append(i['c_end_date'])
v_c_group.append(i['c_group']['_id'])
v_c_public_user.append(i['c_public_user']['_id'])
# v_c_site.append(i['c_site']['_id'])
v_c_skipped.append(i['c_skipped'])
v_c_start_date.append(i['c_start_date'])
v_c_step.append(i['c_step']['_id'])
v_c_study.append(i['c_study']['_id'])
v_c_task.append(i['c_task']['_id'])
v_c_task_response.append(i['c_task']['_id'])
v_c_value.append(i['c_value'])
v_shared.append(i['shared'])
my_dict['type'] = v_type[0]
my_dict['_id'] = v_id[0]
my_dict['object'] = v_object[0]
my_dict['created'] = v_created[0]
my_dict['creator'] = v_creator[0]
my_dict['owner'] = v_owner[0]
my_dict['access'] = v_access[0]
my_dict['accessRoles'] = v_accessroles[0]
my_dict['favorite'] = v_favorite[0]
my_dict['c_account'] = v_c_account[0]
my_dict['c_end_date'] = v_c_end_date[0]
my_dict['c_group'] = v_c_group[0]
my_dict['c_public_user'] = v_c_public_user[0]
# my_dict['c_site'] = v_c_site[0]
my_dict['c_skipped'] = v_c_skipped[0]
my_dict['c_start_date'] = v_c_start_date[0]
my_dict['c_step'] = v_c_step[0]
my_dict['c_study'] = v_c_study[0]
my_dict['c_task'] = v_c_task[0]
my_dict['c_task_response'] = v_c_task_response[0]
my_dict['c_value'] = v_c_value[0]
my_dict['shared'] = v_shared[0]
Now, I dont know how do I loop thorugh whole python dictionary and do it for whole file.
source https://stackoverflow.com/questions/69367165/how-to-create-a-custom-json-file-from-the-existing-json-file-using-python
Comments
Post a Comment