使用python加载json文件导致错误

时间:2017-05-09 09:34:14

标签: json

Goof哀悼,

当我尝试将json文件加载到mongodb时,我收到以下错误:

raise ValueError("No JSON object could be decoded").

在我看来,我的问题来自我的第二个字段,但我不知道如何将“”更改为名称,或者在加载之前将其删除。

我的json文件是:

{ 
"_id" : "585a9ecec62747d1e19497a5", 
"" : NumberInt(0), 
"VendorID" : NumberInt(2), 
"lpep_pickup_datetime" : "2015-11-01 00:57:34", 
"Lpep_dropoff_datetime" : "2015-11-01 23:57:45", 
"Store_and_fwd_flag" : "N", 
"RateCodeID" : NumberInt(5), 
"Pickup_longitude" : -73.9550857544, 
"Pickup_latitude" : 40.6637229919, 
"Dropoff_longitude" : -73.958984375, 
"Dropoff_latitude" : 40.6634483337, 
"Passenger_count" : NumberInt(1), 
"Trip_distance" : 0.09, 
"Fare_amount" : 15.0, 
"Extra" : 0.0, 
"MTA_tax" : 0.0, 
"Tip_amount" : 0.0, 
"Tolls_amount" : 0.0, 
"Ehail_fee" : "", 
"improvement_surcharge" : 0.0, 
"Total_amount" : 15.0, 
"Payment_type" : NumberInt(2), 
"Trip_type" : NumberInt(2), 
"x" : -8232642.48775, 
"y" : 4962866.701, 
"valid_longitude" : NumberInt(1), 
"valid_latitude" : NumberInt(1), 
"valid_coordinates" : NumberInt(2)
}

1 个答案:

答案 0 :(得分:0)

JSON文件中的问题不是空字符串键(允许),而是NumberInt(...)的出现:这在JSON中无效。您需要提供数字而不将其包装在某种功能中。

所以这是有效的:

{
    "_id": "585a9ecec62747d1e19497a5",
    "": 0,
    "VendorID": 2,
    "lpep_pickup_datetime": "2015-11-01 00:57:34",
    "Lpep_dropoff_datetime": "2015-11-01 23:57:45",
    "Store_and_fwd_flag": "N",
    "RateCodeID": 5,
    "Pickup_longitude": -73.9550857544,
    "Pickup_latitude": 40.6637229919,
    "Dropoff_longitude": -73.958984375,
    "Dropoff_latitude": 40.6634483337,
    "Passenger_count": 1,
    "Trip_distance": 0.09,
    "Fare_amount": 15.0,
    "Extra": 0.0,
    "MTA_tax": 0.0,
    "Tip_amount": 0.0,
    "Tolls_amount": 0.0,
    "Ehail_fee": "",
    "improvement_surcharge": 0.0,
    "Total_amount": 15.0,
    "Payment_type": 2,
    "Trip_type": 2,
    "x": -8232642.48775,
    "y": 4962866.701,
    "valid_longitude": 1,
    "valid_latitude": 1,
    "valid_coordinates": 2
}

如果您无法控制非JSON文件,那么在读取文件内容后,请替换NumberInt这样的事件(在Python中):

import re
json = re.sub(r"NumberInt\((\d+)\)", r"\1", json)