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

ValueError: X has 10 features, but LinearRegression is expecting 1 features as input

So, I am trying to predict the model but its throwing error like it has 10 features but it expacts only 1. So I am confused can anyone help me with it? more importantly its not working for me when my friend runs it. It works perfectly fine dose anyone know the reason about it? cv = KFold(n_splits = 10) all_loss = [] for i in range(9): # 1st for loop over polynomial orders poly_order = i X_train = make_polynomial(x, poly_order) loss_at_order = [] # initiate a set to collect loss for CV for train_index, test_index in cv.split(X_train): print('TRAIN:', train_index, 'TEST:', test_index) X_train_cv, X_test_cv = X_train[train_index], X_test[test_index] t_train_cv, t_test_cv = t[train_index], t[test_index] reg.fit(X_train_cv, t_train_cv) loss_at_order.append(np.mean((t_test_cv - reg.predict(X_test_cv))**2)) # collect loss at fold all_loss.append(np.mean(loss_at_order)) # collect loss at order plt.plot(np.log(al...

Sorting large arrays of big numeric stings

I was solving bigSorting() problem from hackerrank: Consider an array of numeric strings where each string is a positive number with anywhere from to digits. Sort the array's elements in non-decreasing, or ascending order of their integer values and return the sorted array. I know it works as follows: def bigSorting(unsorted): return sorted(unsorted, key=int) But I didnt guess this approach earlier. Initially I tried below: def bigSorting(unsorted): int_unsorted = [int(i) for i in unsorted] int_sorted = sorted(int_unsorted) return [str(i) for i in int_sorted] However, for some of the test cases, it was showing time limit exceeded. Why is it so? PS: I dont know exactly what those test cases were as hacker rank does not reveal all test cases. source https://stackoverflow.com/questions/73007397/sorting-large-arrays-of-big-numeric-stings

How to load Javascript with imported modules?

I am trying to import modules from tensorflowjs, and below is my code. test.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title </head> <body> <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@2.0.0/dist/tf.min.js"></script> <script type="module" src="./test.js"></script> </body> </html> test.js import * as tf from "./node_modules/@tensorflow/tfjs"; import {loadGraphModel} from "./node_modules/@tensorflow/tfjs-converter"; const MODEL_URL = './model.json'; const model = await loadGraphModel(MODEL_URL); const cat = document.getElementById('cat'); model.execute(tf.browser.fromPixels(cat)); Besides, I run the server using python -m http.server in my command prompt(Windows 10), and this is the error prompt in the console log of my browser: Failed to loa...