1

I'm trying to load a JSON document into my Python code. The document looks like this:

{
  "FH45 G34": {
    "surname": "Foo",
    "firstName": "Bar",
    "address": "1275 Real Street, FooBar",
    "postCode": "Foo Bar",
    "county": "FooBar"
  },
  ...
}

I'm trying to load the file with the following Python code:

with open("foobar.json", "r") as foo:
        bar = json.load(foo)

However I get the following error ValueError: No JSON object could be decoded

What's wrong?

EDIT: I'm not able to use the SimpleJSON module as it's not available for Python 3.2, which I am stuck using. JSONLint also shows my JSON as being valid.

EDIT 2: The whole file is here

EDIT 3: Somehow the problem fixed itself.

Paul Clavier
  • 291
  • 4
  • 13

2 Answers2

0

When reading the file using Python 2.7, it transforms most lines into unicode strings, which suggests that maybe the following change could make a difference in your Python 3.2 world:

with open('foobar.json', encoding='utf-8') as foo:
        bar = json.load(foo)
        # or
        bar = json.loads(foo.read())

Detect offending line

If the above doesn't work, you need to find offending json code. One way to do this is simply to remove half of the file and try again. If it still fails, then keep on removing half the file.

If it works, replace the file with the failing part, and start removing halfs again. After a few attempts, you'll be able to locate the offending part of the file, or whether you have a basic reading error of sorts.

To eliminate reading errors, you could try doing a print('file: {}'.format(foo.read())) in front of the json.loads() call, just to verify the read contents.

Test json inline

Another test you can try to further eliminate error sources are to take one or a few entries insert into a string and check whether it's the file handling or json handling which gives you the error. Something similar to the following:

json_text= '''
{ "FH45 G34": 
  {
    "surname": "Foo", 
    "firstName": "Bar",
    "address": "1275 Real Street, FooBar", 
    "postCode": "Foo Bar", 
    "county": "FooBar"
  }
}'''

parsed = json.loads(json_text)

And you could also check if it fails due to the space in the key of the first entry. In other words remove the spaces from a failing entry, primarily in the lefthand side key values.

If these tests are OK, you have some issue with your file read. If they fail, you have some issue with the json module.

holroy
  • 3,047
  • 25
  • 41
  • Nope, still not working over in Python 3.2 land. I saved the file as UTF-8 in Notepad, set encoding to UTF-8 and UTF-8-SIG, and tried with only one entry, and I still get the same error. – Paul Clavier Oct 16 '15 at 09:01
  • @PaulClavier, try the inline tests as described in my update – holroy Oct 17 '15 at 00:22
  • @PaulClavier, I've tested with a freshly compiled Python 3.2.6, and can't generate the error. Which exact version of Python are you running, and under which operating system? – holroy Oct 17 '15 at 00:23
  • The inline tests pass – Paul Clavier Oct 23 '15 at 07:59
  • @PaulClavier, if inline test pass, you have some issue with the file reading. Have you tried reading the file, and just doing a print on each line? – holroy Oct 23 '15 at 09:14
0

The following code has been tested and works flawlessly with the whole JSON file as provided in your question.

Working Example — Tested with Python 2.6.9 and 2.7.10 and 3.2.5 and 3.3.5 and 3.5.0

import json

with open('data.json', 'r') as json_data:

    data = json.load(json_data)

    for key, value in data.items():
        print('''
key    {0}

    county       {1}
    surname      {2}
    postCode     {3}
    firstName    {4}
    address      {5}
'''.format(*[key,
             value['county'],
             value['surname'],
             value['postCode'],
             value['firstName'],
             value['address']]))

Output

key    VB95 JGL

    county       FooBar
    surname      Foo
    postCode     Foo Bar
    firstName    Bar
    address      1272 Real Street, FooBar


key    AH63 FIR

    county       FooBar
    surname      Foo
    postCode     Foo Bar
    firstName    Bar
    address      1275 Real Street, FooBar
jesterjunk
  • 2,342
  • 22
  • 18
  • I've already tried that and unfortunaately it didn't work either. `.load()` automatically reads the file for you anyway so it shouldn't matter. `.loads()` is better for when you're parsing say a response from a webpage. – Paul Clavier Oct 19 '15 at 14:16
  • @paul-clavier I've just edited and tested my code to reflect what you mentioned, and ran it on a fresh install of Python 3.2.5 and it works the sames as with all the other versions I have tested it with. It seems that your copy of the json library may be broken/corrupted, or possibly something is wrong with your Python installation. – jesterjunk Oct 19 '15 at 14:42
  • @paul-clavier Can you provide the exact release version of Python 3.2 that you are using? 3.2.0 or 3.2.1 or 3.2.2 or 3.2.3 or 3.2.4 or 3.2.5 or 3.2.6 ? You can get your Python version with the following line `import sys; print('.'.join(map(str, sys.version_info[0:3])))` – jesterjunk Oct 19 '15 at 16:45
  • @PaulClavier, Have you tried the last test I suggested of using `json.loads(multiline_text_variable)` to check whether it is your file loading which fails, or the actual json library failing? – holroy Oct 21 '15 at 20:12