简体   繁体   English

ValueError:预期为 object 或将 json 读取为 pandas dataframe 时的值

[英]ValueError: Expected object or value when reading json as pandas dataframe

Sample 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"
}

I tried:我试过了:

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

getting error ValueError: Expected object or value收到错误 ValueError: Expected object or value

also

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

error: json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 509)错误:json.decoder.JSONDecodeError:额外数据:第 2 行第 1 列(字符 509)

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

error: json.decoder.JSONDecodeError: Extra data: line 1 column 577 (char 576)错误:json.decoder.JSONDecodeError:额外数据:第 1 行第 577 列(字符 576)

How to read this json into pandas如何将这个 json 读成 pandas

I got the same error, read the function documentation and play around with different parameters.我遇到了同样的错误,阅读函数文档并使用不同的参数。

I solved it by using the one below,我通过使用下面的一个解决了它,

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

you can try out other things like你可以尝试其他的东西,比如

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

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

Your JSON is malformed.您的 JSON 格式错误。

ValueError: Expected object or value can occur if you mistyped the file name. ValueError: Expected object or value如果您输入错误的文件名,可能会出现ValueError: Expected object or value Does Data.json exist? Data.json是否存在? I noticed for your other attempts you used gdb.json .我注意到您在其他尝试中使用了gdb.json

Once you confirm the file name is correct, you have to fix your JSON.确认文件名正确后,您必须修复 JSON。 What you have now is two disconnected records separated by a space.您现在拥有的是两个由空格分隔的断开连接的记录。 Lists in JSON must be valid arrays inside square brackets and separated by a comma: [{record1}, {record2}, ...] JSON 中的列表必须是方括号内的有效数组并用逗号分隔: [{record1}, {record2}, ...]

Also, for pandas you should put your array under a root element called "data" :此外,对于熊猫,您应该将数组放在名为"data"的根元素下:

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

Your JSON should end up looking like this:您的 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"
    }]}

Finally, pandas calls this format split orientation , so you have to load it as follows:最后,pandas 将此格式称为split orientation ,因此您必须按如下方式加载它:

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

df now contains the following data frame: 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

你应该确保终端目录与文件目录相同(当我发生这个错误时,因为我使用了vscode,对我来说意味着vscode中的终端目录与我想要的python文件不同执行)

I dont think this would be the problem as it should be the default (I think).我不认为这会是问题,因为它应该是默认值(我认为)。 But have you tried this?但是你试过这个吗? Adding an 'r' to specify the file is read only.添加“r”以指定文件是只读的。

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

make your path easy, it will be helpful to read data.使您的路径变得轻松,这将有助于阅读数据。 meanwhile, just put your file on your desktop and give that path to read the data.同时,只需将您的文件放在桌面上并提供读取数据的路径。 It works.有用。

You can try to change relative path to absolute path For your situation change您可以尝试将相对路径更改为绝对路径针对您的情况更改

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

to

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

I got the same error when I run the same code from jupyter notebook to pycharm's jupyter notebook in console当我在控制台中从 jupyter notebook 运行相同的代码到 pycharm 的 jupyter notebook 时,我遇到了同样的错误

Another variation, combining tips from the thread that all failed independently but this worked for me:另一种变体,结合了线程中所有独立失败的提示,但这对我有用:

pd.read_json('file.json', lines=True, encoding = 'utf-8-sig')

If you type in the absolute path of and use \ it should work.如果您输入绝对路径并使用 \ 它应该可以工作。 At least thats how I fixed the issue至少我就是这样解决问题的

I am not sure if I clearly understood your question, you just trying to read json data ?我不确定我是否清楚地理解了您的问题,您只是想读取 json 数据?

I just collected your sample data into list as shown below我刚刚将您的样本数据收集到列表中,如下所示

[
  {
   "_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"
}
]

and ran this code并运行此代码

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

Output:-输出:-

              _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]

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

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

I encountered this error message today, and in my case the problem was that the encoding of the text file was UTF-8-BOM instead of UTF-8, which is the default for read_json().我今天遇到了这个错误消息,就我而言,问题是文本文件的编码是 UTF-8-BOM 而不是 UTF-8,这是 read_json() 的默认值。 This can be solved by specifying the encoding:这可以通过指定编码来解决:

data= pd.read_json('Data.json', encoding = 'utf-8-sig')

I faced the same problem the reason behind this is the json file has something that doesn't abide by json rules.我遇到了同样的问题,这背后的原因是 json 文件有一些不遵守 json 规则的东西。 In my case i had used single quotes in one of the values instead of double quotes.在我的情况下,我在其中一个值中使用了单引号而不是双引号。

在此处输入图片说明

this worked for me: pd.read_json('./dataset/healthtemp.json', typ="series")这对我有用: pd.read_json('./dataset/healthtemp.json', typ="series")

every thing is ok except for one thing除了一件事,一切都很好

in the.json file put the code below:在 .json 文件中输入以下代码:

{
"a": {
    "_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"
},
"b": {
    "_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"
}
}

Thank you谢谢

The problem of ValueError: All arrays must be of the same length that happens with ValueError: All arrays must be of the same length

df = pd.read_json (r'./filename.json')#,lines=True)

can be solved by changing the line above to the following.可以通过将上面的行更改为以下行来解决。

df = pd.read_json (r'./filename.json',lines=True)

I just solved this problem by adding a "/" at the beggining of the absolute path.我只是通过在绝对路径的开头添加一个“/”来解决这个问题。

import pandas as pd    
pd_from_json = pd.read_json("/home/miguel/folder/information.json")

Seems like there's a million things that can cause this.似乎有一百万种事情会导致这种情况。 In my case, it was that my json file started had a byte order mark, denoted by [BOM] [unix] in the vim-airline.在我的例子中,我的 json 文件开始有一个字节顺序标记,在 vim-airline 中用[BOM] [unix]表示。 I don't know what the byte order mark is or when it would be needed.我不知道字节顺序标记是什么或何时需要它。 To remove that, in vim, I ran :set nobomb and then saved the file.为了删除它,在 vim 中,我运行了:set nobomb然后保存了文件。 Then, pandas could read it and I was good to go.然后,pandas 可以读取它,我对 go 很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pandas 打开 JSON 文件为 DataFrame: ValueError: Expected ZA8CFDE6331BD59EB2AC96F8911C4 or value - Pandas to open JSON File as DataFrame : ValueError: Expected object or value ValueError: Expected object or value &lt;-&gt; Can't load a json file to pandas dataframe, or convert to csv, either will suffice - ValueError: Expected object or value <-> Can't load a json file to pandas dataframe, or convert to csv, either will suffice ValueError:使用参数lines = True读取熊猫中的json时的预期对象或值 - ValueError: Expected object or value when reading json in panda using parameter lines=True pandas read_json 返回 ValueError: Expected object or value - pandas read_json returns ValueError: Expected object or value Python Pandas ValueError:预期的对象或值 - Python Pandas ValueError: Expected object or value 使用Pandas读取JSON时的“预期字符串或Unicode” - 'Expected String or Unicode' when reading JSON with Pandas ValueError:预期的对象或值 - ValueError: Expected object or value ValueError:应为 object 或值 - 读取打开的 Recipes 数据 - ValueError: Expected object or value - reading open Recipes data 读取json文件返回ValueError:期望的对象或值 - Read json file return a ValueError: Expected object or value ValueError:预期是dict或pandas.DataFrame - ValueError: expected a dict or pandas.DataFrame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM