robertg
robertg

Reputation: 83

Unable to parse valid json file in Python

I am trying to parse a json file using Python3.6 and json module. Unfortunately, I get this error:

json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

I tried with both json.load() and json.loads() methods, but I still have this error. I do not understand from where this error comes from, due to the fact that my JSON does not have single quotes.

The JSON is the next one:

{
    "stats": {
        "host1": {
            "changed": 0,
            "failures": 0,
            "ok": 1,
            "skipped": 0,
            "unreachable": 0
        },
        "host2": {
            "changed": 0,
            "failures": 0,
            "ok": 1,
            "skipped": 0,
            "unreachable": 0
        },
        "host3": {
            "changed": 0,
            "failures": 0,
            "ok": 1,
            "skipped": 0,
            "unreachable": 0
        },
        "host4": {
            "changed": 0,
            "failures": 0,
            "ok": 0,
            "skipped": 0,
            "unreachable": 1
        }
    }
}

And my python code is the next one:

import json

json_file = open("example.json", "r")
data = json.load(json_file)

I tried other solutions found here, but no one worked for me. Any suggestion/solution is highly apreciated.

Upvotes: 1

Views: 1459

Answers (1)

Robᵩ
Robᵩ

Reputation: 168626

Your JSON file is encoded UTF-16-LE, but you are reading it with the default encoding.

Try this:

json_file = open("example.json", "r", encoding='utf_16_le')

Upvotes: 5

Related Questions