I have 3 objects that I am populating into a JSON file format. The objects come from an API which needs rolled out to access as such:
my_dict = {}
for elem_a in list_a:
for elem_b in elem_a:
for elem_c in elem_b:
elem_c_info = {
"name" : elem_c.prop1,
"ID" : elem_c.prop2,
"GPA" : elem_c.prop3
}
my_dict.setdefault("university", {}) \
.setdefault(str(elem_a), {}) \
.setdefault(str(elem_b), {}) \
.setdefault("student", []).append(elem_c_info)
Produces output like this:
{
"university": {
"universityA": {
"class_1": {
"student": [
{
"name": "student_1",
"ID": "1234",
"GPA": "3.8"
},
My desired output:
{
"university": [{
"name": "universityA",
"class": [{
"name": "class_1",
"student": [{
"name": "student_1",
"ID": "1234",
"GPA": "3.8"
},
{
"name": "student_2",
"ID": "12345",
"GPA": "3.4"
}
]
},
{
"name": "class_2",
"student": [{
"name": "student_3",
"ID": "14",
"GPA": "3.0"
}]
}
]
}]
}
As you can see, I need each nested dictionary enclosed in a list at each level and I need to add a key/value pair at each level (except the inner-most). I was successful at handling the inner-most nest with .append but it's not working higher up. Any suggestions on adding these elements to the data structure? Accepting solutions to either or both issues.
Here is what the input data structures look like running a simple nested loop (credit to @PM_2Ring for the suggestion):
In my example above:
list_a = [obj_1, obj_2, ..., obj_n]
for elem_a in list_a:
print('A', elem_a.name)
for elem_b in elem_a:
print(' B', elem_b.name)
for elem_c in elem_b:
print(' C', elem_c.prop1, elem_c.prop2, elem_c.prop3)
('A', universityA)
(' B', class_1)
(' C', student_1, 12345, 3.8)
(' B', class_2)
(' C', student_2, 145, 3.6)
('A', universityB)
(' B', class_1)
(' C', student_1, 12345, 3.8)
(' C', student_2, 1235, 3.6)
(' C', student_3, 12345, 3.4)
(' B', class_2)
(' C', student_1, 145, 3.6)
....
elem_a = [universityA, universityB, universityC] # Top tier
elem_b = [universityA.class_1, universityA.class_2, universityA.class_3] # Middle tier with OO property 'name'
elem_c = [universityA.class_1.student_name, universityA.class_1.student_id, universityA.class_1.student_gpa] # bottom tier relationship
I feel like your approach fails because you try to do everything at once: that one-liner assignment is rather non-trivial. Consider instead something like this:
my_dict = {}
list_result = []
for elem_a in list_a:
elem_a_result = []
for elem_b in elem_a:
elem_b_result = []
for elem_c in elem_b:
elem_b_result.append({
'prop1': elem_c.prop1,
'prop2': elem_c.prop2,
'prop3': elem_c.prop3,
})
elem_a_result.append({
'name': elem_b.name,
'elem_b': elem_b_result,
})
list_result.append({
'name': elem_a.name,
'elem_a': elem_a_result
})
my_dict = {
'list_a': list_result
}
You can make the code in every for-loop only work on a specific level of your structure and not care about the big picture.
Note: I didn't test this because I don't have access to data formatted like yours (with attributes and iteration and everything).
After pondering on it for a while I see that this can be easily rewritten as a huge list comprehension. Whether this is more readable is however questionable. It looks much cleaner and resembles the target structure in shape, but it is somewhat more convoluted than the straightforward for-loop version.
my_dict = {
'list_a': [
{
'name': elem_a.name,
'elem_a': [
{
'name': elem_b.name,
'elem_b': [
{
'prop1': elem_c.prop1,
'prop2': elem_c.prop2,
'prop3': elem_c.prop3,
}
for elem_c in elem_b]
}
for elem_b in elem_a]
}
for elem_a in list_a]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With