0

I am trying to parse the following student grade report JSON using Python

{
    "report":[
        {
            "enrollment": "rit2011001",
            "name": "Julia",
            "subject":[
                {
                    "code": "DSA",
                    "grade": "A"
                }
            ]
        },
        {
            "enrollment": "rit2011020",
            "name": "Samantha",
            "subject":[
                {
                    "code": "COM",
                    "grade": "B"
                },
                {
                    "code": "DSA",
                    "grade": "A"
                }
            ]
        }
    ]
}

Such that the report should be ordered ascending first by code, then by grade and then by enrollment. Here is how the output should be

COM B rit2011020 Samantha
DSA A rit2011001 Julia
DSA A rit2011020 Samantha

Here is my incomplete piece of code that I need help with:

import json

data='''{
    "report":[
        {
            "enrollment": "rit2011001",
            "name": "Julia",
            "subject":[
                {
                    "code": "DSA",
                    "grade": "A"
                }
            ]
        },
        {
            "enrollment": "rit2011020",
            "name": "Samantha",
            "subject":[
                {
                    "code": "COM",
                    "grade": "B"
                },
                {
                    "code": "DSA",
                    "grade": "A"
                }
            ]
        }
    ]
}'''

print data  #for debug
parsed_json = json.loads(data)
print parsed_json #for debug
for key,value in sorted(parsed_json.items()):
    print key,value

I don't know how to apply successive filtering to achieve the result.

2
  • Does this help you? stackoverflow.com/questions/4110665/… Commented Jan 15, 2019 at 2:59
  • @jmoney I already looked at the example and tried following it but I got Errors Commented Jan 15, 2019 at 3:02

2 Answers 2

1

Try using a nested loop, with a print:

import json
data='''{
    "report":[
        {
            "enrollment": "rit2011001",
            "name": "Julia",
            "subject":[
                {
                    "code": "DSA",
                    "grade": "A"
                }
            ]
        },
        {
            "enrollment": "rit2011020",
            "name": "Samantha",
            "subject":[
                {
                    "code": "COM",
                    "grade": "B"
                },
                {
                    "code": "DSA",
                    "grade": "A"
                }
            ]
        }
    ]
}'''
print data  #for debug
parsed_json = json.loads(data)
print parsed_json #for debug
for i in parsed_json['report']:
    for x in i['subject']:
        print x['code'],x['grade'],i['enrollment'],i['name']

Output:

DSA A rit2011001 Julia
COM B rit2011020 Samantha
DSA A rit2011020 Samantha

If care about the order of the frame:

import json
data='''{
    "report":[
        {
            "enrollment": "rit2011001",
            "name": "Julia",
            "subject":[
                {
                    "code": "DSA",
                    "grade": "A"
                }
            ]
        },
        {
            "enrollment": "rit2011020",
            "name": "Samantha",
            "subject":[
                {
                    "code": "COM",
                    "grade": "B"
                },
                {
                    "code": "DSA",
                    "grade": "A"
                }
            ]
        }
    ]
}'''
print data  #for debug
parsed_json = json.loads(data)
print parsed_json #for debug
l=[]
for i in parsed_json['report']:
    for x in i['subject']:
        l.append(' '.join([x['code'],x['grade'],i['enrollment'],i['name']]))
print('\n'.join(sorted(l)))
Sign up to request clarification or add additional context in comments.

Comments

1

If you are willing to use a very popular external library for data analysis then you can use pandas with json_normalize(), e.g.:

In []:
from pandas.io.json import json_normalize

df = json_normalize(parsed_json['report'], 'subject', ['enrollment', 'name'])
df.sort_values(['code', 'grade', 'enrollment']).reset_index(drop=True)

Out[]:
  code grade  enrollment      name
0  COM     B  rit2011020  Samantha
1  DSA     A  rit2011001     Julia
2  DSA     A  rit2011020  Samantha

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.