0

I was ask to extract data from a JSON file using Python. The original JSON file looks like this:

{"votes": {"funny": 15, "useful": 48, "cool": 18}, "user_id": "JkeCKyEaQlbLd9uZYl4DjA", "name": "LiiLii C.", "url": "http://www.yelp.com/user_details?userid=JkeCKyEaQlbLd9uZYl4DjA", "average_stars": 3.2258064516128999, "review_count": 31, "type": "user"}

{"votes": {"funny": 0, "useful": 2, "cool": 0}, "user_id": "cs91PAsv6esdWAaSkzm2lg", "name": "Jan Ellen T.", "url": "http://www.yelp.com/user_details?userid=cs91PAsv6esdWAaSkzm2lg", "average_stars": 3.0, "review_count": 4, "type": "user"}

And I was asked to use Python to extract this file and give an output file that looks like this:

name    city    state   stars   review_count    main_category

Southern California Medical Group   Los Angeles CA  3.5 2   Medical Centers

Harvard Square Shiatsu  Cambridge   MA  4.0 4   Massage

Faith & Glory Collective    Kitchener   ON  4.0 2   Tattoo

Von's Records & Posters West Lafayette  IN  3.5 3   Music & DVDs

JP's Java   Austin  TX  3.5 85  Food

I am really confused by this assignment because I don't understand why the output file has nothing similar to the original JSON file. And is this because I need to extract data from the JSON file through each provided link? And if this is the case, how should I do this task?

Kara
  • 6,115
  • 16
  • 50
  • 57
Parker
  • 193
  • 2
  • 3
  • 14
  • This sounds like a question you should ask to the person that gave the assignment. You are right, this is confusing. Maybe he just wants you to find the categories of the data in the JSON and parse it in a similar manner? – Matthew Nov 07 '16 at 20:50

1 Answers1

0

If you're accurately quoting what has been asked, you need to gather more details about the users mentioned in your JSON, in order to compile the required output. Yelp has an API which might help you gather the required data, given a user ID. Anyhow, doesn't look like this is really a JSON question.

Prateek Dewan
  • 1,587
  • 3
  • 16
  • 29
  • The actual quote is "Write Python code to extract data about all businesses from the file yelp_academic_dataset.json. Note that "business categories" are actually a list in the JSON file, so we'll just use the first element of this list as the main_category for a business." – Parker Nov 07 '16 at 20:51
  • What I am really confused is that how do I obtain information in the output file from the original JSON file – Parker Nov 07 '16 at 20:51
  • So what you just shared was incomplete data. You'd need to provide the entire yelp_academic_dataset.json to get any help I guess. – Prateek Dewan Nov 07 '16 at 20:54
  • Thanks, I just checked the file and found that the "business" related info are at the bottom of this JSON file. If they are at the bottom, how should I extract this part of data and not the top part? – Parker Nov 07 '16 at 20:59
  • if it's a proper json, the entire data should be in a key-value format. load the file into a json variable in python: – Prateek Dewan Nov 07 '16 at 21:02
  • import json data = json.loads(open('yelp_academic_dataset.json','r').read()) print data.keys() this might help figure out the structure of the data. – Prateek Dewan Nov 07 '16 at 21:03
  • One more question. After I have those lists, such as name, city, state, how can I convert them to a csv file with these names in the first row? – Parker Nov 08 '16 at 02:38
  • already answered here http://stackoverflow.com/questions/10373247/how-do-i-write-a-python-dictionary-to-a-csv-file – Prateek Dewan Nov 08 '16 at 12:29