Home > Blockchain >  Iterating Thru Two JSON Payloads with multiple loops in Python - Creating a dictionary from results
Iterating Thru Two JSON Payloads with multiple loops in Python - Creating a dictionary from results

Time:08-09

Hello Fellow Pythoineers, I am a little confused by this output Goal - I have two loops iterating thru two JSON payloads.

  • 1st Loop groups all Department keys into a list of unique values.
  • 2nd Loop iterates thru the key (which should match the keys collected from the first loop, and then assigns that key a display name and key to a dictionary

Example 1st Loop Returns key SO 2nd Loop Finds SO and determines the displayName is SOCIOLOGY the dict should return SO: SOCIOLOGY

import json

all_courses = open('methodist_all_courses.json')
all_courses_json = json.load(all_courses)

all_departments = open('departments_payload.json')
all_departments_json = json.load(all_departments)

departments =[];
department_long = dict()

for cd_crs_id, course_details in all_courses_json.items():
    for department_code in course_details['departments']:
        if department_code not in departments:
            departments.append(department_code)


for depot_code, departmnt_details in all_courses_json.items():
    for distilled_department_code in departments:
        if depot_code == distilled_department_code:
            department_long[distilled_department_code] = departmnt_details['displayName']

print(department_long)

Replit: https://replit.com/join/tafkwzqaya-terry-brooksjr

Payload for loop 1: https://pastebin.com/9Bu0XL3Y Payload for loop 2: https://pastebin.com/s0kMSmaF

Output

Traceback (most recent call last):
  File "main.py", line 7, in <module>
    all_departments_json = json.load(all_departments)
  File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/json/__init__.py", line 293, in load
    return loads(fp.read(),
  File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/json/decoder.py", line 340, in decode
    raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 879 column 2 (char 25384)

CodePudding user response:

departments_payload.json is not a valid json file because it ends with an extra character.

}\

Delete that ending \ (or if you pull that from a site, just use slicing the get not include that final character.

Secondly, your code won't give you the desired output as you are iterating both loops through all_courses_json.

This will work though once you fix that json.

Code:

import json

all_courses = open('methodist_all_courses.json')
all_courses_json = json.load(all_courses)

all_departments = open('departments_payload.json')
all_departments_json = json.load(all_departments)


departments =[];
department_long = dict()

for cd_crs_id, course_details in all_courses_json.items():
    for department_code in course_details['departments']:
        if department_code not in departments:
            departments.append(department_code)


for depot_code, departmnt_details in all_departments_json.items():
    for distilled_department_code in departments:
        if depot_code == distilled_department_code:
            department_long[distilled_department_code] = departmnt_details['displayName']

print(department_long)

Output:

print(department_long)
{'AC': 'Accounting', 'AR': 'Art', 'AT': 'Athletic Training', 'BI': 'Biology', 'BU': 'Business Administration', 'CH': 'Chemistry and Physical Science', 'CM': 'Communication', 'CS': 'Computer Science', 'EC': 'Economics', 'ED': 'Education', 'EG': 'Engineering', 'LL': 'English,Literature, Languages, and Culture', 'EV': 'Environmental Management', 'HC': 'Health Care Administration', 'HI': 'History', 'HO': 'Honors', 'ID': 'Inderdisciplinary Studies', 'JU': 'Justice Studies', 'KI': 'Kinesiology', 'MK': 'Marketing', 'MB': 'Masters of Business Admninistration', 'ME': 'Masters of Education', 'MA': 'Mathematics', 'MI': 'Military Science', 'NU': 'Nursing', 'OT': 'Occupational Therapy', 'PF': 'Performing Arts', 'PH': 'Philosophy and Religion', 'DP': 'Physical Therapy', 'PA': 'Physician Assistant', 'GS': 'Political Science', 'PG': 'Professional Golf Management', 'PT': 'Professional Tennis Management', 'PS': 'Psychology', 'RM': 'Resort Management', 'SW': 'Social Work', 'SM': 'Sport Management', 'TE': 'Teacher Education'}
  • Related