Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python JSON parser error: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I need some help parsing JSON file. I've tried a couple of different ways to get the data I need. Below is a sample of the code and also a section of the JSON data but when I run the code I get the error listed above.

There's 500K lines of text in the JSON and it first fails about about 1400 lines in and I can't see anything in that area section to indicate why.

I've run it successfully by only checking blocks of JSON up to the first 1400 lines and I've used a different parser and got the same error.

I'm debating if it's an error in the code, an error in the JSON or a result of the JSON being made of different kids of data as some (like the example below) is for a forklift and others for fixed machines but it is all structured just like below.

All help sincerely appreciate.

Code:

import json

file_list = ['filename.txt'] #insert filename(s) here

for x in range(len(file_list)):

    with open(file_list[x], 'r') as f:
        distros_dict = json.load(f)

#list the headlines to be parsed
for distro in distros_dict:
    print(distro['name'], distro['positionTS'], distro['smoothedPosition'][0], distro['smoothedPosition'][1], distro['smoothedPosition'][2])

And here is a section of the JSON:

{
    "id": "b4994c877c9c",
    "name": "Trukki_0001",
    "areaId": "Tracking001",
    "areaName": "Ajoneuvo",
    "color": "#FF0000",
    "coordinateSystemId": "CoordSys001",
    "coordinateSystemName": null,
    "covarianceMatrix": [
        0.47,
        0.06,
        0.06,
        0.61
    ],
    "position": [
        33.86,
        33.07,
        2.15
    ],
    "positionAccuracy": 0.36,
    "positionTS": 1489363199493,
    "smoothedPosition": [
        33.96,
        33.13,
        2.15
    ],
    "zones": [
        {
            "id": "Zone001",
            "name": "Halli1"
        }
    ],
    "direction": [
        0,
        0,
        0
    ],
    "collisionId": null,
    "restrictedArea": "",
    "tagType": "VEHICLE_MANNED",
    "drivenVehicleId": null,
    "drivenByEmployeeIds": null,
    "simpleXY": "33|33",
    "EventProcessedUtcTime": "2017-03-13T00:00:00.3175072Z",
    "PartitionId": 1,
    "EventEnqueuedUtcTime": "2017-03-13T00:00:00.0470000Z"
}
like image 271
exmatelote Avatar asked Apr 06 '26 03:04

exmatelote


1 Answers

The actual problem was that the JSON file was coded in UTF not ASCII. If you change the encoding using something like notepad++ then it will be solved.

like image 161
exmatelote Avatar answered Apr 08 '26 19:04

exmatelote



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!