JSONDecodeError: Extra data: line 1 column 228 (char 227)

主宰稳场 提交于 2020-03-03 09:10:42

问题


I am using Ipython to do some data analysis, I can't load the JSON file. Please help me to load this JSON file in IPython. And I also want to skip same words in the first line to make it a clean format, I want each record looks like this :

{"station_id":"72","num_bikes_available":18,"num_bikes_disabled":0,"num_docks_available":20,"num_docks_disabled":1,"is_installed":1,"is_renting":1,"is_returning":1,"last_reported":"1467164372","eightd_has_available_keys":false},

Here is my code:

In [9]: path = 'stationstatus.json'

In [10]: records = [json.loads(line) for line in open(path)]

Here is the error:

JSONDecodeError                           Traceback (most recent call last)
<ipython-input-10-b1e0b494454a> in <module>()
----> 1 records = [json.loads(line) for line in open(path)]  
<ipython-input-10-b1e0b494454a> in <listcomp>(.0)
----> 1 records = [json.loads(line) for line in open(path)]
//anaconda/lib/python3.5/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
317             parse_int is None and parse_float is None and
318             parse_constant is None and object_pairs_hook is None and not kw):
--> 319         return _default_decoder.decode(s)
320     if cls is None:
321         cls = JSONDecoder
//anaconda/lib/python3.5/json/decoder.py in decode(self, s, _w)
340         end = _w(s, end).end()
341         if end != len(s):
--> 342             raise JSONDecodeError("Extra data", s, end)
343         return obj
344 

Here is part of my JSON file** :

{
  "last_updated": 1467164806,
  "ttl": 10,
  "data": {
    "stations": [{
        "station_id": "72",
        "num_bikes_available": 18,
        "num_bikes_disabled": 0,
        "num_docks_available": 20,
        "num_docks_disabled": 1,
        "is_installed": 1,
        "is_renting": 1,
        "is_returning": 1,
        "last_reported": "1467164372",
        "eightd_has_available_keys": false
    }, {
        "station_id": "79",
        "num_bikes_available": 1,
        "num_bikes_disabled": 2,
        "num_docks_available": 30,
        "num_docks_disabled": 0,
        "is_installed": 1,
        "is_renting": 1,
        "is_returning": 1,
        "last_reported": "1467163375",
        "eightd_has_available_keys": false
    }, {
        "station_id": "82",
        "num_bikes_available": 3,
        "num_bikes_disabled": 3,
        "num_docks_available": 21,
        "num_docks_disabled": 0,
        "is_installed": 1,
        "is_renting": 1,
        "is_returning": 1,
        "last_reported": "1467161631",
        "eightd_has_available_keys": false
    }, {
        "station_id": "83",
        "num_bikes_available": 36,
        "num_bikes_disabled": 0,
        "num_docks_available": 26,
        "num_docks_disabled": 0,
        "is_installed": 1,
        "is_renting": 1,
        "is_returning": 1,
        "last_reported": "1467163453",
        "eightd_has_available_keys": false
    }, {
        "station_id": "116",
        "num_bikes_available": 5,
        "num_bikes_disabled": 3,
        "num_docks_available": 31,
        "num_docks_disabled": 0,
        "is_installed": 1,
        "is_renting": 1,
        "is_returning": 1,
        "last_reported": "1467164693",
        "eightd_has_available_keys": false
    }, {
        "station_id": "119",
        "num_bikes_available": 15,
        "num_bikes_disabled": 0,
        "num_docks_available": 4,
        "num_docks_disabled": 0,
        "is_installed": 1,
        "is_renting": 1,
        "is_returning": 1,
        "last_reported": "1467160413",
        "eightd_has_available_keys": false
    }]
  }
}

回答1:


Here is a suggestion for loading the file:

with open('Path/to/file', 'r') as content_file:
    content = content_file.read()
records = json.loads(content)

The root object in your json will be in your records variable



来源:https://stackoverflow.com/questions/38577399/jsondecodeerror-extra-data-line-1-column-228-char-227

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!