1

The json file content as follows:

{"votes": {"funny": 0, "useful": 5, "cool": 2}, "user_id": "rLtl8ZkDX5vH5nAx9C3q5Q", "review_id": "fWKvX83p0-ka4JS3dc6E5A", "stars": 5, "date": "2011-01-26", "text": "My wife took me here on my birthday for breakfast and it was excellent.  It looked like the place fills up pretty quickly so the earlier you get here the better.\n\nDo yourself a favor and get their Bloody Mary. It came with 2 pieces of their griddled bread with was amazing and it absolutely made the meal complete.  It was the best \"toast\" I've ever had.\n\nAnyway, I can't wait to go back!", "type": "review", "business_id": "9yKzy9PApeiPPOUJEtnvkg"}
{"votes": {"funny": 0, "useful": 0, "cool": 0}, "user_id": "0a2KyEL0d3Yb1V6aivbIuQ", "review_id": "IjZ33sJrzXqU-0X6U8NwyA", "stars": 5, "date": "2011-07-27", "text": "I have no idea why some people give bad reviews about this place. It goes to show you, you can please everyone. They are probably griping about something that their own fault... but they said we'll be seated when the girl comes back from seating someone else. So, everything was great and not like these bad reviewers. That goes to show you that  you have to try these things yourself because all these bad reviewers have some serious issues.", "type": "review", "business_id": "ZRJwVLyzEJq1VAihDhYiow"}

my code is:

import json
from pprint import pprint
review = open('/User/Desktop/python/test.json')
data = json.load(review)
pprint(data["votes"])

The error is:

Traceback (most recent call last):
  File "/Users/hadoop/Documents/workspace/dataming-course/src/Yelp/main.py", line 8, in <module>
    data = json.load(review)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 278, in load
    **kw)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 326, in loads
    return _default_decoder.decode(s)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 363, in decode
    raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 2 column 1 - line 3 column 1 (char 623 - 1294)
7
  • Here is one way you could fix this particular set of data (by putting each JSON piece into an array): jsonfiddle.net/7skmq (Try taking a look at the "Source" box on the example link, if you'd like :) Commented Apr 28, 2013 at 4:00
  • 1
    @summea you help me a lot, really really thank you!!! Commented Apr 28, 2013 at 4:18
  • Glad it worked out for you :) Commented Apr 28, 2013 at 4:19
  • @summea oppos~ it still report error I don't know why Commented Apr 28, 2013 at 4:25
  • Is it possible to have your new JSON text posted in your question? Commented Apr 28, 2013 at 4:48

3 Answers 3

5

You have two JSON documents in a single file. Consider putting them into an array or something. The top-level of the file should only contain a single element.

Sign up to request clarification or add additional context in comments.

Comments

2

If you can't change the input file, you may use JSONDecoder.raw_decode to do it in chunks.

>>> dec = json.JSONDecoder()
>>> dec.raw_decode('["a",1]{"foo": 2}')
(['a', 1], 7)
>>> dec.raw_decode('["a",1]{"foo": 2}', 7)
({'foo': 2}, 17)

You will need to read the file to a string first.

2 Comments

can you explain about these two lines of code, I am still confused~
@JoJo The raw_decode method will return the first object plus the index from where you should start looking if there's more data. That way, you can store multiple json objects in the same string/file
1

For what it's worth, you could try putting your JSON into an array, like this:

[ { "business_id" : "9yKzy9PApeiPPOUJEtnvkg",
    "date" : "2011-01-26",
    "review_id" : "fWKvX83p0-ka4JS3dc6E5A",
    "stars" : "5",
    "text" : "My wife took me here on my birthday for breakfast and it was excellent.  It looked like the place fills up pretty quickly so the earlier you get here the better.\n\nDo yourself a favor and get their Bloody Mary. It came with 2 pieces of their griddled bread with was amazing and it absolutely made the meal complete.  It was the best \"toast\" I've ever had.\n\nAnyway, I can't wait to go back!",
    "type" : "review",
    "user_id" : "rLtl8ZkDX5vH5nAx9C3q5Q",
    "votes" : { "cool" : "2",
        "funny" : "0",
        "useful" : "5"
      }
  },
  { "business_id" : "ZRJwVLyzEJq1VAihDhYiow",
    "date" : "2011-07-27",
    "review_id" : "IjZ33sJrzXqU-0X6U8NwyA",
    "stars" : "5",
    "text" : "I have no idea why some people give bad reviews about this place. It goes to show you, you can please everyone. They are probably griping about something that their own fault... but they said we'll be seated when the girl comes back from seating someone else. So, everything was great and not like these bad reviewers. That goes to show you that  you have to try these things yourself because all these bad reviewers have some serious issues.",
    "type" : "review",
    "user_id" : "0a2KyEL0d3Yb1V6aivbIuQ",
    "votes" : { "cool" : "0",
        "funny" : "0",
        "useful" : "0"
      }
  }
]

(And do note the , that separates the two "main" parts of the JSON array :)

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.