ValueError:将json作为pandas dataframe读取时的预期对象或值

时间:2017-05-31 14:06:46

标签: python json pandas

示例数据

{
   "_id": "OzE5vaa3p7",
   "categories": [
      {
         "__type": "Pointer",
         "className": "Category",
         "objectId": "nebCwWd2Fr"
      }
   ],
   "isActive": true,
   "imageUrl": "https://firebasestorage.googleapis.com/v0/b/shopgro-1376.appspot.com/o/Barcode%20Data%20Upload%28II%29%2FAnil_puttu_flour_500g.png?alt=media&token=9cf63197-0925-4360-a31a-4675f4f46ae2",
   "barcode": "8908001921015",
   "isFmcg": true,
   "itemName": "Anil puttu flour 500g",
   "mrp": 58,
   "_created_at": "2016-10-02T13:49:03.281Z",
   "_updated_at": "2017-02-22T08:48:09.548Z"
}

{
   "_id": "ENPCL8ph1p",
   "categories": [
      {
         "__type": "Pointer",
         "className": "Category",
         "objectId": "B4nZeUHmVK"
      }
   ],
   "isActive": true,
   "imageUrl": "https://firebasestorage.googleapis.com/v0/b/kirananearby-9eaa8.appspot.com/o/Barcode%20data%20upload%2FYippee_Magic_Masala_Noodles,_70_g.png?alt=media&token=d9e47bd7-f847-4d6f-9460-4be8dbcaae00",
   "barcode": "8901725181222",
   "isFmcg": true,
   "itemName": "Yippee Magic Masala Noodles, 70 G",
   "mrp": 12,
   "_created_at": "2016-10-02T13:49:03.284Z",
   "_updated_at": "2017-02-22T08:48:09.074Z"
}

我试过了:

import pandas as pd
data= pd.read_json('Data.json')
  

获取错误ValueError:预期的对象或值

import json
with open('gdb.json') as datafile:
    data = json.load(datafile)
retail = pd.DataFrame(data)
  

错误:json.decoder.JSONDecodeError:额外数据:第2行第1列(char 509)

with open('gdb.json') as datafile:
for line in datafile:
    data = json.loads(line)
retail = pd.DataFrame(data)
  

错误:json.decoder.JSONDecodeError:额外数据:第1行第577行(字符576)

如何将这个json读成pandas

8 个答案:

答案 0 :(得分:2)

我得到了同样的错误,请阅读函数documentation并使用不同的参数。

我使用下面的那个来解决它,

data= pd.read_json('Data.json', lines=True)

你可以试试其他的东西,比如

data= pd.read_json('Data.json', lines=True, orient='records')

data= pd.read_json('Data.json', orient=str)

答案 1 :(得分:1)

您可以尝试将相对路径更改为绝对路径 对于你的情况 改变

import pandas as pd
data= pd.read_json('Data.json')

import pandas as pd
data= pd.read_json('C://Data.json')#the absolute path in explore

当我在控制台中从jupyter笔记本到pycharm的jupyter笔记本运行相同的代码时,我遇到了相同的错误

答案 2 :(得分:0)

您应该确保终端目录与文件目录相同(当我遇到此错误时,因为我使用的是vscode,这对我来说意味着vscode中的终端目录与我的python文件不同我要执行的

答案 3 :(得分:0)

我不确定我是否清楚地理解了您的问题,您只是尝试读取json数据?

我刚刚将您的示例数据收集到如下所示的列表中

[
  {
   "_id": "OzE5vaa3p7",
   "categories": [
      {
         "__type": "Pointer",
         "className": "Category",
         "objectId": "nebCwWd2Fr"
      }
   ],
   "isActive": true,
   "imageUrl": "https://firebasestorage.googleapis.com/v0/b/shopgro-1376.appspot.com/o/Barcode%20Data%20Upload%28II%29%2FAnil_puttu_flour_500g.png?alt=media&token=9cf63197-0925-4360-a31a-4675f4f46ae2",
   "barcode": "8908001921015",
   "isFmcg": true,
   "itemName": "Anil puttu flour 500g",
   "mrp": 58,
   "_created_at": "2016-10-02T13:49:03.281Z",
   "_updated_at": "2017-02-22T08:48:09.548Z"
},
{
   "_id": "ENPCL8ph1p",
   "categories": [
      {
         "__type": "Pointer",
         "className": "Category",
         "objectId": "B4nZeUHmVK"
      }
   ],
   "isActive": true,
   "imageUrl": "https://firebasestorage.googleapis.com/v0/b/kirananearby-9eaa8.appspot.com/o/Barcode%20data%20upload%2FYippee_Magic_Masala_Noodles,_70_g.png?alt=media&token=d9e47bd7-f847-4d6f-9460-4be8dbcaae00",
   "barcode": "8901725181222",
   "isFmcg": true,
   "itemName": "Yippee Magic Masala Noodles, 70 G",
   "mrp": 12,
   "_created_at": "2016-10-02T13:49:03.284Z",
   "_updated_at": "2017-02-22T08:48:09.074Z"
}
]

并运行此代码

import pandas as pd
df = pd.read_json('Data.json')
print(df)

输出:-

              _created_at ... mrp
0 2016-10-02 13:49:03.281 ...  58
1 2016-10-02 13:49:03.284 ...  12

[2 rows x 10 columns]

答案 4 :(得分:0)

您的JSON格式错误。

如果键入错误的文件名,可能会出现

ValueError: Expected object or valueData.json是否存在?我注意到您使用gdb.json的其他尝试。

一旦确认文件名正确,就必须修复JSON。您现在拥有的是两个未连接的记录,中间用空格隔开。 JSON中的列表必须是方括号内的有效数组,并用逗号分隔:[{record1}, {record2}, ...]

另外,对于大熊猫,您应该将数组放在名为"data"的根元素下:

{ "data": [ {record1}, {record2}, ... ] }

您的JSON应该最终看起来像这样:

{"data":
    [{
        "_id": "OzE5vaa3p7",
        "categories": [
            {
                "__type": "Pointer",
                "className": "Category",
                "objectId": "nebCwWd2Fr"
            }
        ],
        "isActive": true,
        "imageUrl": "https://firebasestorage.googleapis.com/v0/b/shopgro-1376.appspot.com/o/Barcode%20Data%20Upload%28II%29%2FAnil_puttu_flour_500g.png?alt=media&token=9cf63197-0925-4360-a31a-4675f4f46ae2",
        "barcode": "8908001921015",
        "isFmcg": true,
        "itemName": "Anil puttu flour 500g",
        "mrp": 58,
        "_created_at": "2016-10-02T13:49:03.281Z",
        "_updated_at": "2017-02-22T08:48:09.548Z"
    }
    ,
    {
        "_id": "ENPCL8ph1p",
        "categories": [
            {
                "__type": "Pointer",
                "className": "Category",
                "objectId": "B4nZeUHmVK"
            }
        ],
        "isActive": true,
        "imageUrl": "https://firebasestorage.googleapis.com/v0/b/kirananearby-9eaa8.appspot.com/o/Barcode%20data%20upload%2FYippee_Magic_Masala_Noodles,_70_g.png?alt=media&token=d9e47bd7-f847-4d6f-9460-4be8dbcaae00",
        "barcode": "8901725181222",
        "isFmcg": true,
        "itemName": "Yippee Magic Masala Noodles, 70 G",
        "mrp": 12,
        "_created_at": "2016-10-02T13:49:03.284Z",
        "_updated_at": "2017-02-22T08:48:09.074Z"
    }]}

最后,熊猫将这种格式称为split orientation,因此您必须按以下方式加载它:

df = pd.read_json('gdb.json', orient='split')

df现在包含以下数据框:

          _id                                                   categories  isActive                                                     imageUrl        barcode  isFmcg                           itemName  mrp                      _created_at                      _updated_at
0  OzE5vaa3p7  [{'__type': 'Pointer', 'className': 'Category', 'objectI...      True  https://firebasestorage.googleapis.com/v0/b/shopgro-1376...  8908001921015    True              Anil puttu flour 500g   58 2016-10-02 13:49:03.281000+00:00 2017-02-22 08:48:09.548000+00:00
1  ENPCL8ph1p  [{'__type': 'Pointer', 'className': 'Category', 'objectI...      True  https://firebasestorage.googleapis.com/v0/b/kirananearby...  8901725181222    True  Yippee Magic Masala Noodles, 70 G   12 2016-10-02 13:49:03.284000+00:00 2017-02-22 08:48:09.074000+00:00

答案 5 :(得分:0)

使您的路径变得容易,这将有助于读取数据。 同时,只需将文件放在桌面上,然后指定读取数据的路径即可。可以。

答案 6 :(得分:0)

如果您尝试以下代码,它将解决问题:

data_set = pd.read_json(r'json_file_address\file_name.json', lines=True)

答案 7 :(得分:-1)

我不认为这会是问题,因为它应该是默认值(我认为)。但你试过这个吗?添加“r”以指定文件是只读的。

import json with open('gdb.json', 'r') as datafile: data = json.load(datafile) retail = pd.DataFrame(data)