1

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.

6
  • 3
    I can't reproduce this error. It works fine for me, using Python 3.6 Commented Mar 6, 2018 at 21:40
  • 4
    You say your JSON looks "something" like that, can you post the actual thing? Commented Mar 6, 2018 at 21:41
  • The error message says the error is in the first line. Can you at least post the actual first line? Additionally, if you are on Linux/UNIX can you post hd example.json | head -1 Commented Mar 6, 2018 at 21:47
  • @chrisz I just updated the post with the original json. I didn`t realized that the initial one was actually working. Commented Mar 6, 2018 at 21:54
  • No issues with that the updated one either for me. Commented Mar 6, 2018 at 21:56

1 Answer 1

5

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')
Sign up to request clarification or add additional context in comments.

1 Comment

Great catch! For the curious, I imagine this is how he knew: column 2 (char 1). The only way this would be the case is if it's encoded in UTF-16.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.