Error in reading JSON

Asked

Viewed 835 times

1

I am trying to read the Jsons from a folder and get the proper values, I have tested all the Jsons to see if they were valid. The folder with the Jsons is called 'test'

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

The idea is to go through the entire folder and all the files, reading them and showing the contents. However when running I get one:

Valueerror: No JSON Object could be decoded

This is a JSON:

{
    "test": "Search User 1",
    "url": "http://127.0.0.1:8000/api/v1/user/1/?format=json",
    "status_code": 200,
    "method": "get"
}
  • @Cesarmiguel can send me the link from where I find how to make this formwork that you made for me?

  • If you notice when creating/editing a question/answer, you have an editor to edit and manipulate your question by placing text as code or citation. I couldn’t find any link that explains it, but just explore a little and you’ll see. I also leave a link here to see (it’s always worth it): http://meta.pt.stackoverflow.com/questions/1084/comordevemos-formatter-questions-e-replies

1 answer

0


As you are reading from a file, possibly the content is a string, then json.load will not work. The other thing is that you are passing to the method json.load the file descriptor, not a JSON itself.

Use instead:

json_data = json.loads(fd.read())

Read more here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.