I have two datasets as following and which are in Json format.
Emp_DT = json.dumps({'Employee_Details': [j1]}, indent=2, sort_keys=True)
Emp_HR = json.dumps({'Employee_HR_Hierarchy': [j2]}, indent=2, sort_keys=True)
Now i want to combine these two to have a single Json output with Object name "Employee".
Below are the two datasets which I want to combine together to get a consolidated JSon output
print(Emp_DT)
"Employee_Details":
{
"Name": "John Smith",
"Age": 32,
"Department": "Sales",
"Salary": 30000,
"DOJ": 12-06-2020
}
print(Emp_HR)
"Employee_HR_Hierarchy":
{
"Emp Name": "John Smith",
"1st Manager Name": "Richard",
"2nd Manager Name": "Paul"
}
CodePudding user response:
This code:
import json
Emp_DT = {
"Name": "John Smith",
"Age": 32,
"Department": "Sales",
"Salary": 30000,
"DOJ": "12-06-2020"
}
Emp_HR = {
"Emp Name": "John Smith",
"1st Manager Name": "Richard",
"2nd Manager Name": "Paul"
}
emp1 = {
"Employee_Details": Emp_DT,
"Employee_HR_Hierarchy": Emp_HR
}
emps= {}
emps["Employee"] = emp1
emps_json_string = json.dumps(emps, indent=4)
print(emps_json_string)
Produces this output:
{
"Employee": {
"Employee_Details": {
"Name": "John Smith",
"Age": 32,
"Department": "Sales",
"Salary": 30000,
"DOJ": "12-06-2020"
},
"Employee_HR_Hierarchy": {
"Emp Name": "John Smith",
"1st Manager Name": "Richard",
"2nd Manager Name": "Paul"
}
}
}
Which is what you have asked for (?)
In addition, I would recommend formatting the code with meaningful names and a logical structure. For example, since the data here is of one employee, I would have named Emp_DT
as employee_1_details
and Emp_HR
as employee_1_HR
. And when saving it to the dictionary of all employees, it is probably better to go for emps["employee_1"] = emp1
instead of emps["Employee"] = emp1
.
Finally, I would recommend reading PEP8 when you have time.