简体   繁体   中英

loading valid json file in python throws error JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I have a json file, that is valid according to json linter, that looks like this:

[{
    "Device": {
        "BNCode": "BN2003612",
        "DeviceID": 3700,
        "ModelName": "2004c",
        "VariantName": "2004c",
        "supplier": {
            "SupplierCode": "ALCATEL",
            "SupplierID": 3,
            "SupplierName": "Alcatel",
            "created_at": "2019-08-14T14:47:00",
            "last_updated_by": "",
            "updated_at": "2019-08-14T14:47:00"
        }
    },
    "MarketPlayer": {
        "DetailedMarketPlayer": "WestCom",
        "MarketPlayerID": 3968,
        "MarketPlayerName": "Amazon"
    },
    "PriceUrl": "",
    "Sales_Price": {
        "CurrencyCode": "EUR",
        "EUR": 6.658333333333334e+001,
        "local_currency": 6.658333333333334e+001
    },
    "ScreenshotUrl": "",
    "Source": "OFFICIENCE",
    "TimeStamp": "Date"
}, {
    "Device": {
        "BNCode": "BN2003833",
        "DeviceID": 3800,
        "ModelName": "Nokia 130",
        "VariantName": "Nokia 130 8MB LTE DS",
        "supplier": {
            "SupplierCode": "HMD",
            "SupplierID": 45,
            "SupplierName": "Hmd",
            "created_at": "2019-08-14T14:47:00",
            "last_updated_by": "",
            "updated_at": "2019-08-14T14:47:00"
        }
    },
    "MarketPlayer": {
        "DetailedMarketPlayer": "MediaMarkt",
        "MarketPlayerID": 3815,
        "MarketPlayerName": "MediaMarkt"
    },
    "PriceUrl": "",
    "Sales_Price": {
        "CurrencyCode": "EUR",
        "EUR": 2.665833333333333e+001,
        "local_currency": 2.665833333333333e+001
    },
    "ScreenshotUrl": "",
    "Source": "OFFICIENCE",
    "TimeStamp": "Date"
}]

To import the json file into python I am using the following code:

import json
with open('prices_json.txt') as f:
    data_dict = json.load(f)

I am getting the error: JSONDecodeError: Expecting value: line 1 column 1 (char 0).

When I use the json.loads function I get the error: TypeError: the JSON object must be str, bytes or bytearray, not TextIOWrapper.

EDIT: I think I noticed that the JSON is getting "fixed" when copying the data from the file directly into here and formatting it as code. Thats why you guys can not reproduce my error. I attached this image of how the code actually looks like, it seems there is more whitespace and I would assume that is causing the issues so any idea on how to fix it is welcome. 在此处输入图片说明

Link to the file: https://file.io/yKw7tp

If you are using the above prices_json.txt file then you should be getting it worked. I have tested it and it is working. See this repl .

You can also try passing complete file as string to json.loads() method to convert it into json object.

data_dict = json.loads(f.read())

EDIT

The actual cause of the problem was the file itself. When i tried to print it using f.read() , it gave output like this:

ÿþ[ {

         " D e v i c e " :   {

                 " B N C o d e " :   " B N 2 0 0 3 6 1 2 " ,

                 " D e v i c e I D " :   3 7 0 0 ,

                 " M o d e l N a m e " :   " 2 0 0 4 c " ,

                 " V a r i a n t N a m e " :   " 2 0 0 4 c " ,

                 " s u p p l i e r " :   {

                         " S u p p l i e r C o d e " :   " A L C A T E L " ,

                         " S u p p l i e r I D " :   3 ,

                         " S u p p l i e r N a m e " :   " A l c a t e l " ,

                         " c r e a t e d _ a t " :   " 2 0 1 9 - 0 8 - 1 4 T 1 4 : 4 7 : 0 0 " ,

                         " l a s t _ u p d a t e d _ b y " :   " " ,

                         " u p d a t e d _ a t " :   " 2 0 1 9 - 0 8 - 1 4 T 1 4 : 4 7 : 0 0 "

                 }

         } ,

         " M a r k e t P l a y e r " :   {

                 " D e t a i l e d M a r k e t P l a y e r " :   " W e s t C o m " ,

                 " M a r k e t P l a y e r I D " :   3 9 6 8 ,

                 " M a r k e t P l a y e r N a m e " :   " A m a z o n "

         } ,

         " P r i c e U r l " :   " " ,

         " S a l e s _ P r i c e " :   {

                 " C u r r e n c y C o d e " :   " E U R " ,

                 " E U R " :   6 . 6 5 8 3 3 3 3 3 3 3 3 3 3 3 4 e + 0 0 1 ,

                 " l o c a l _ c u r r e n c y " :   6 . 6 5 8 3 3 3 3 3 3 3 3 3 3 3 4 e + 0 0 1

         } ,

         " S c r e e n s h o t U r l " :   " " ,

         " S o u r c e " :   " O F F I C I E N C E " ,

         " T i m e S t a m p " :   " D a t e "

 } ,   {

         " D e v i c e " :   {

                 " B N C o d e " :   " B N 2 0 0 3 8 3 3 " ,

                 " D e v i c e I D " :   3 8 0 0 ,

                 " M o d e l N a m e " :   " N o k i a   1 3 0 " ,

                 " V a r i a n t N a m e " :   " N o k i a   1 3 0   8 M B   L T E   D S " ,

                 " s u p p l i e r " :   {

                         " S u p p l i e r C o d e " :   " H M D " ,

                         " S u p p l i e r I D " :   4 5 ,

                         " S u p p l i e r N a m e " :   " H m d " ,

                         " c r e a t e d _ a t " :   " 2 0 1 9 - 0 8 - 1 4 T 1 4 : 4 7 : 0 0 " ,

                         " l a s t _ u p d a t e d _ b y " :   " " ,

                         " u p d a t e d _ a t " :   " 2 0 1 9 - 0 8 - 1 4 T 1 4 : 4 7 : 0 0 "

                 }

         } ,

         " M a r k e t P l a y e r " :   {

                 " D e t a i l e d M a r k e t P l a y e r " :   " M e d i a M a r k t " ,

                 " M a r k e t P l a y e r I D " :   3 8 1 5 ,

                 " M a r k e t P l a y e r N a m e " :   " M e d i a M a r k t "

         } ,

         " P r i c e U r l " :   " " ,

         " S a l e s _ P r i c e " :   {

                 " C u r r e n c y C o d e " :   " E U R " ,

                 " E U R " :   2 . 6 6 5 8 3 3 3 3 3 3 3 3 3 3 3 e + 0 0 1 ,

                 " l o c a l _ c u r r e n c y " :   2 . 6 6 5 8 3 3 3 3 3 3 3 3 3 3 3 e + 0 0 1

         } ,

         " S c r e e n s h o t U r l " :   " " ,

         " S o u r c e " :   " O F F I C I E N C E " ,

         " T i m e S t a m p " :   " D a t e "

 } ]

You need to use utf-16 as encoding parameter while opening the file to fix the problem.

Using json.loads method:

import json
with open('prices_json.txt',encoding="utf-16") as f:
    data_dict = json.loads(f.read())
print(data_dict)

Using json.load method

import json
with open('prices_json.txt',encoding="utf-16") as f:
    data_dict = json.load(f)
print(data_dict)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM