I’m doing a course and I’m a little complicated with this exercise
I have 2 json that I have to join in a dictionary, but this must meet certain rules.
department = [
{"ID": 1, "Name": "John", "Role": "Manager", "employees" :[
{"ID": 4, "Name": "Mike", "Role": "Engineer"}]},
{"ID": 3, "Name": "Sam", "Role": "Manager", "employees" :[]
}]
employee = { "ID": [2],"Name": "Jack", "Role": "Analyst"}
1- The employee of Identifier 2 is under the charge of the Manager with Identifier 3.
2- The Database where the information will be ingested does not support columns of different data types.
To do this, a function must be generated that corrects this in the inputs.
3- The expected output must be a nested dictionary
How can I manage to join these fulfilling all conditions?
And the expected result should be this:
4
Answers
You can update your json directly as:
I believe the best way to do it is:
Because other employee IDs are integers (not lists), you need to extract the first item from the "ID" key to fix the input data.
You can use the next function (or a loop) to find department 3’s dictionary in the list (hard coding the position in the list seemed like cheating to me)
Then append the whole employee dictionary to the "employees" list in department 3
result:
You could use a function like this to enforce datatypes:
You can loop through the departments until you find the right one and then add
employee
after processing each value in it withformat_empData
:After that,
department
should look like