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 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: Additional data: line 2 column 1 (char 509)
with open('gdb.json') as datafile:
for line in datafile:
data = json.loads(line)
retail = pd.DataFrame(data)
error: json.decoder.JSONDecodeError: Additional data: line 1 column 577 (char 576)
How to read this json in pandas
source to share
I got the same error, read the function documentation and played around with different parameters.
I solved it with the one below,
data= pd.read_json('Data.json', lines=True)
You can try other things like
data= pd.read_json('Data.json', lines=True, orient='records')
data= pd.read_json('Data.json', orient=str)
source to share
I'm not sure if I understood your question clearly, are you just trying to read the JSON data?
I have just compiled your sample data into a 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]
source to share
Your JSON is mangled.
ValueError: Expected object or value
can happen if you misspelled the filename. Does it exist Data.json
? I noticed that for your other attempts, you used gdb.json
.
Once you confirm that the filename is correct, you should correct your JSON. You now have two unrelated records separated by a space. Lists in JSON must be valid arrays in square brackets and separated by commas:[{record1}, {record2}, ...]
Also, for pandas, you have to put your array on the root element named "data"
:
{ "data": [ {record1}, {record2}, ... ] }
Your JSON should look like this:
{"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 like this:
df = pd.read_json('gdb.json', orient='split')
df
now contains the following dataframe:
_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
source to share