Skip to main content

How to create a custom JSON file from the existing JSON file using python? [closed]

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

Popular posts from this blog

Confusion between commands.Bot and discord.Client | Which one should I use?

Whenever you look at YouTube tutorials or code from this website there is a real variation. Some developers use client = discord.Client(intents=intents) while the others use bot = commands.Bot(command_prefix="something", intents=intents) . Now I know slightly about the difference but I get errors from different places from my code when I use either of them and its confusing. Especially since there has a few changes over the years in discord.py it is hard to find the real difference. I tried sticking to discord.Client then I found that there are more features in commands.Bot . Then I found errors when using commands.Bot . An example of this is: When I try to use commands.Bot client = commands.Bot(command_prefix=">",intents=intents) async def load(): for filename in os.listdir("./Cogs"): if filename.endswith(".py"): client.load_extension(f"Cogs.{filename[:-3]}") The above doesnt giveany response from my Cogs ...

Where and how is this Laravel kernel constructor called? [closed]

Where and how is this Laravel kernel constructor called? public fucntion __construct(Application $app, $Router $roouter) { } I have read the documentation and some online tutorial but I can find any clear explanation. I am learning Laravel and I am wondering where does this kernel constructor receives its arguments from. "POSTMOTERM" CLARIFICATION: Here is more clarity.I have checked the boostrap/app.php and it is only used for boostrapping the interfaces into the container class. What is not clear to me is where and how the Kernel class is instatiated and the arguments passed to the object calling the constructor.Something similar to; obj = new kernel(arg1,arg2) or, is the framework using some magic functions somewhere? Special gratitude to those who burn their eyeballs and brain cells on this trivia before it goes into a full blown menopause alias "MARKED AS DUPLICATE". To some of the itchy-finger keyboard warriors, a.k.a The mods,because I believe in th...

Why is my reports service not connecting?

I am trying to pull some data from a Postgres database using Node.js and node-postures but I can't figure out why my service isn't connecting. my routes/index.js file: const express = require('express'); const router = express.Router(); const ordersCountController = require('../controllers/ordersCountController'); const ordersController = require('../controllers/ordersController'); const weeklyReportsController = require('../controllers/weeklyReportsController'); router.get('/orders_count', ordersCountController); router.get('/orders', ordersController); router.get('/weekly_reports', weeklyReportsController); module.exports = router; My controllers/weeklyReportsController.js file: const weeklyReportsService = require('../services/weeklyReportsService'); const weeklyReportsController = async (req, res) => { try { const data = await weeklyReportsService; res.json({data}) console...