I have a table which has some sample data as provided below
import pandas as pd
data = {
"mode": ["single_table_list", "single_table_list", "single_table_list", "relational_table_list", "relational_table_list"],
"type": ["type_a", "type_b", "type_c", "parent_table", "child_table"],
"file_name": ["file_a", "file_b", "file_c", "file_d", "file_e"],
"file_path": ["path_a", "path_b", "path_c", "path_d", "path_e"],
"file_sample": ["sample_a", "sample_b", "sample_c", "sample_d", "sample_e"],
"target_file_name": ["tgt_name_a", "tgt_name_b", "tgt_name_c", "tgt_name_d", "tgt_name_e"],
"target_file_path": ["tgt_path_a", "tgt_path_b", "tgt_path_c", "tgt_path_d", "tgt_path_e"],
"child_table": ["","","","child_d",""],
"parent_key": ["","","","key_d",""],
"parent_table": ["","","","","parent_e"],
"child_key": ["","","","","key_e"]
}
I am facing difficulty with the output into the required formatting, how do I go about implementing this so that it provides the output as shown below.
Is it possible to have the JSON output structured as such?
{
"single_table_list": {
"1":{
"type": "type_a",
"file_name": "file_a",
"file_path": "path_a",
"file_sample": "sample_a",
"target_file_name": "tgt_name_a",
"target_file_path": "tgt_path_a"
},
"2":{
"type": "type_b",
"file_name": "file_b",
"file_path": "path_b",
"file_sample": "sample_b",
"target_file_name": "tgt_name_b",
"target_file_path": "tgt_path_b"
},
"3":{
"type": "type_c",
"file_name": "file_c",
"file_path": "path_c",
"file_sample": "sample_c",
"target_file_name": "tgt_name_c",
"target_file_path": "tgt_path_c"
}
},
"relational_table_list": {
"1":{
"parent_table_list":{
"type": "type_parent",
"file_name": "file_d",
"file_path": "path_d",
"file_sample": "sample_d",
"target_file_name": "tgt_name_d",
"target_file_path": "tgt_path_d",
"child_table": "child_d"
"parent_key": "key_d"
},
"child_table_list":{
"type": "type_child",
"file_name": "file_e",
"file_path": "path_e",
"file_sample": "sample_e",
"target_file_name": "tgt_name_e",
"target_file_path": "tgt_path_e",
"parent_table": "parent_e",
"child_key": "key_e"
}
}
}
}
2
Answers
you can use "json" module
json.dumps is uesd to convert python Object to JSON String.And you should give argument"indent". there is a example
I don’t sure what you want
but I think you want to organizes the data according to the desired structure, and then converts it to a JSON string with proper indentation. The output should match your specified JSON structure.