10

I'm trying to read a json and get its values. I have a folder with the JSON's archives, and I need to open all archives and get the values from them.

This is the code:

# -*- encoding: utf-8 -*-
from pprint import pprint
import json
import os 
def start():
    for dirname, dirnames, filenames in os.walk('test'):
        for filename in filenames:
            json_file = open(os.path.join(dirname, filename)).read()
            # json_file = unicode(json_file, 'utf-8')
            json_data = json.loads(json_file)
           pprint(json_data)
            for key, value in json_data.items():
                print "KEY : ", key
                print "VALUE: ", value
                start()

This is one of the JSON's

{ "test" : "Search User 1",
   "url"  : "http://127.0.0.1:8000/api/v1/user/1/?format=json",
   "status_code" : 200,
   "method" : "get"
}

But when I run it, i get this:

ValueError: No JSON object could be decoded

What the hell is wrong? Yesterday it was working exactly as it is now, or am I crazy

I tried this way:

from pprint import pprint
import json
import os
for dirname, dirnames, filenames in os.walk('test'):
    for filename in filenames:
        json_file_contents = open(os.path.join(dirname, filename)).read()
        try:
            json_data = json.loads(json_file_contents)
        except ValueError, e:
            print e
            print "ERROR"

I cant see any error '-'

for filename in filenames:
        with open(os.path.join(dirname,filename)) as fd:
            json_data = fd.read()
            print json_data

This way I can see what the json files contain, but I can't use for example access by the key, like json_data['url']

Colonel Thirty Two
  • 23,953
  • 8
  • 45
  • 85
Nikolas Daroit
  • 113
  • 1
  • 1
  • 5

6 Answers6

15

For me it was an encoding problem, you can try using Notepad++ to edit your .json file and change the Encoding to UTF-8 without BOM. Another thing you could check is if your json script is valid

Ella Cohen
  • 1,375
  • 1
  • 10
  • 14
12

It's possible the .read() method is moving the cursor to the end of the file. Try:

for filename in filenames:
    with open(os.path.join(dirname,filename)) as fd:
        json_data = json.load(fd)

and see where that gets you.

This, of course, assumes you have valid JSON, as your example demonstrates. (Look out for trailing commas)

Curtis Mattoon
  • 4,642
  • 2
  • 27
  • 34
3

I resolved this error by Converting the json file to UTF-8 with no BOM. Below is a python snippet and url for conversion

myFile=open(cases2.json, 'r')
myObject=myFile.read()
u = myObject.decode('utf-8-sig')
myObject = u.encode('utf-8')
myFile.encoding
myFile.close()
myData=json.loads(myObject,'utf-8')
user2092402
  • 115
  • 9
1

The reply suggesting that .read() was moving the cursor led to a resolution of my version of the problem. I changed

print response.read()
...
json_data = json.loads(response.read())

to

responseStr = response.read()
print responseStr
...
json_data = json.loads(responseStr)
Jerry K.
  • 500
  • 5
  • 9
0

I had the same problem today. Trying to understand the cause, I found this issue related to json module:

http://bugs.python.org/issue18958

Check if the file is UTF8 encoded and if it is the case, then use codecs module to open and read it or just skip the BOM (byte order mark).

waltersantosf
  • 346
  • 3
  • 5
0

Try using this in your ajax/$http with JSON data

contentType: "application/json; charset=utf-8"

sam ruben
  • 365
  • 2
  • 6