【发布时间】:2018-02-05 11:36:35
【问题描述】:
我正在尝试从文本文件中读取 json。我可以将文本文件转换为 json,但有时它会为某些 json 数据引发此错误。 Extra data: line 2 column 1 (char 876): JSONDecodeError.
这是错误堆栈跟踪。
Extra data: line 2 column 1 (char 876): JSONDecodeError
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 28, in lambda_handler
d = json.loads(got_text)
File "/var/lang/lib/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/var/lang/lib/python3.6/json/decoder.py", line 342, in decode
raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 876)
这是代码。
retr = s3_client.get_object(Bucket=bucket, Key=key)
bytestream = BytesIO(retr['Body'].read())
got_text = GzipFile(mode='rb', fileobj=bytestream).read().decode('utf-8')
print(got_text)
d = json.loads(got_text)
print("json output")
print(d)
这是 json。
{
"_metadata": {
"bundled": [
"Segment.io"
],
"unbundled": []
},
"anonymousId": "98cc0c53-jkhjkhj-42d5-8ee1-08a6d6f4e774",
"context": {
"library": {
"name": "analytics.js",
"version": "3.2.5"
},
"page": {
"path": "/login",
"referrer": "http://localhost:8000/",
"search": "",
"title": "Sign in or Register | Your Platform Name Here",
"url": "http://localhost:8000/login"
},
"userAgent": "Mozilla/5.0 ",
"ip": "67.67.88.68"
},
"integrations": {},
"messageId": "ajs-dfbdfbdfbdb",
"properties": {
"path": "/login",
"referrer": "http://localhost:8000/",
"search": "",
"title": "Sign in or Register | Your Platform Name Here",
"url": "http://localhost:8000/login"
},
"receivedAt": "2018-02-05T09:21:02.539Z",
"sentAt": "2018-02-05T09:21:02.413Z",
"timestamp": "2018-02-05T09:21:02.535Z",
"type": "page",
"userId": "16",
"channel": "client",
"originalTimestamp": "2018-02-05T09:21:02.409Z",
"projectId": "dfbfbdfb",
"version": 2
}
可能是什么问题?
【问题讨论】:
-
你能发布你的 JSON 数据吗?
-
这个错误通常意味着文本文件包含json对象之外的一些其他数据。检查你的 json 文本文件一次
-
@Rakesh 发布了 json 数据
-
这个 json 有很多错误。尝试去jsonformatter.org并验证错误在哪里@GaneshKumar