Python json.loads shows ValueError: Extra data

后端 未结 9 809
甜味超标
甜味超标 2020-11-22 16:21

I am getting some data from a JSON file \"new.json\", and I want to filter some data and store it into a new JSON file. Here is my code:

import json
with ope         


        
相关标签:
9条回答
  • 2020-11-22 16:34

    As you can see in the following example, json.loads (and json.load) does not decode multiple json object.

    >>> json.loads('{}')
    {}
    >>> json.loads('{}{}') # == json.loads(json.dumps({}) + json.dumps({}))
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "C:\Python27\lib\json\__init__.py", line 338, in loads
        return _default_decoder.decode(s)
      File "C:\Python27\lib\json\decoder.py", line 368, in decode
        raise ValueError(errmsg("Extra data", s, end, len(s)))
    ValueError: Extra data: line 1 column 3 - line 1 column 5 (char 2 - 4)
    

    If you want to dump multiple dictionaries, wrap them in a list, dump the list (instead of dumping dictionaries multiple times)

    >>> dict1 = {}
    >>> dict2 = {}
    >>> json.dumps([dict1, dict2])
    '[{}, {}]'
    >>> json.loads(json.dumps([dict1, dict2]))
    [{}, {}]
    
    0 讨论(0)
  • 2020-11-22 16:34

    This may also happen if your JSON file is not just 1 JSON record. A JSON record looks like this:

    [{"some data": value, "next key": "another value"}]
    

    It opens and closes with a bracket [ ], within the brackets are the braces { }. There can be many pairs of braces, but it all ends with a close bracket ]. If your json file contains more than one of those:

    [{"some data": value, "next key": "another value"}]
    [{"2nd record data": value, "2nd record key": "another value"}]
    

    then loads() will fail.

    I verified this with my own file that was failing.

    import json
    
    guestFile = open("1_guests.json",'r')
    guestData = guestFile.read()
    guestFile.close()
    gdfJson = json.loads(guestData)
    

    This works because 1_guests.json has one record []. The original file I was using all_guests.json had 6 records separated by newline. I deleted 5 records, (which I already checked to be bookended by brackets) and saved the file under a new name. Then the loads statement worked.

    Error was

       raise ValueError(errmsg("Extra data", s, end, len(s)))
    ValueError: Extra data: line 2 column 1 - line 10 column 1 (char 261900 - 6964758)
    

    PS. I use the word record, but that's not the official name. Also, if your file has newline characters like mine, you can loop through it to loads() one record at a time into a json variable.

    0 讨论(0)
  • 2020-11-22 16:34

    My json file was formatted exactly as the one in the question but none of the solutions here worked out. Finally I found a workaround on another Stackoverflow thread. Since this post is the first link in Google search, I put the that answer here so that other people come to this post in the future will find it more easily.

    As it's been said there the valid json file needs "[" in the beginning and "]" in the end of file. Moreover, after each json item instead of "}" there must be a "},". All brackets without quotations! This piece of code just modifies the malformed json file into its correct format.

    https://stackoverflow.com/a/51919788/2772087

    0 讨论(0)
  • 2020-11-22 16:41

    I came across this because I was trying to load a JSON file dumped from MongoDB. It was giving me an error

    JSONDecodeError: Extra data: line 2 column 1
    

    The MongoDB JSON dump has one object per line, so what worked for me is:

    import json
    data = [json.loads(line) for line in open('data.json', 'r')]
    
    0 讨论(0)
  • 2020-11-22 16:41

    One-liner for your problem:

    data = [json.loads(line) for line in open('tweets.json', 'r')]
    
    0 讨论(0)
  • 2020-11-22 16:43

    You can just read from a file, jsonifying each line as you go:

    tweets = []
    for line in open('tweets.json', 'r'):
        tweets.append(json.loads(line))
    

    This avoids storing intermediate python objects. As long as your write one full tweet per append() call, this should work.

    0 讨论(0)
提交回复
热议问题