0

I'm pretty lost. Not going to lie. I'm trying to figure out how to parse JSON data from the college scorecard API into an HTML file. I used Python to store the JSON data in a dictionary, but other than that, I'm pretty dang lost. How would you write an example sending this data to an HTML file?

def main():
    url = 'https://api.data.gov/ed/collegescorecard/v1/schools.json'
    payload = {
        'api_key': "api_key_string",
        '_fields': ','.join([
            'school.name',
            'school.school_url',
            'school.city',
            'school.state',
            'school.zip',
            '2015.student.size',
        ]),
        'school.operating': '1',
        '2015.academics.program_available.assoc_or_bachelors': 'true',
        '2015.student.size__range': '1..',
        'school.degrees_awarded.predominant__range': '1..3',
        'school.degrees_awarded.highest__range': '2..4',
        'id': '240444',
    }
    data = requests.get(url, params=payload).json()
    for result in data['results']:
        print result

main()

Output:

{u'school.city': u'Madison', u'school.school_url': u'www.wisc.edu', u
'school.zip': u'53706-1380', u'2015.student.size': 29579, u'school.st
ate': u'WI', u'school.name': u'University of Wisconsin-Madison'}

Edit: For clarification, I need to insert the return data to an HTML file that formats and removes data styling and places it onto a table.

Edit II: Json2html edit

data = requests.get(url, params=payload).json()
for result in data['results']:
    print result

    data_processed = json.loads(data)
    formatted_table = json2html.convert(json = data_processed)

    index= open("index.html","w")
    index.write(formatted_table)
    index.close()

Edit: Json2html output:

Output image here

3
  • 1
    You say "send this data to a HTML file". Thats quite an ambiguous question. Do you have a server that is serving the HTML files? Do you simply want to insert the returned data in a HTML file that neatly formats and removes the data type styling and places it into a table for example? Basically, can you be more specific and or expand on "put the data in a HTML file" please? Commented Aug 21, 2018 at 18:19
  • 1
    Ah, my apologies. I need to insert the return data to an HTML file that formats and removes data styling and places it onto a table. I'll edit the question. Commented Aug 21, 2018 at 18:28
  • Thanks. The other guy beat me to the answer. tips hat Commented Aug 21, 2018 at 18:44

1 Answer 1

1

Try using the json2html module! This will convert the JSON that was returned into a 'human readable HTML Table representation'.

This code will take your JSON output and create the HTML:

data_processed = json.loads(data)
formatted_table = json2html.convert(json = data_processed)

Then to save it as HTML you can do this:

your_file= open("filename","w")
your_file.write(formatted_table)
your_file.close()
Sign up to request clarification or add additional context in comments.

14 Comments

Hey thanks Jen. I'll give it a shot and see what happens.
Actually, what do you think about using Pandas instead of json2html?
You can use Pandas to create a table but you cannot directly input that into an HTML file. To turn a Pandas data frame into an HTML table you can use DataFrame.to_html().
Yeah, that's what I did. I did "DataFrame.to_html()" and while everything printed out on the VM, I didn't get any input on an HTML file.
Can you edit your post with the code that you tried and I will take a look?
|

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.