Module' Object Has No Attribute 'Loads' While Parsing JSON Using Python

module' object has no attribute 'loads' while parsing JSON using python

File "json.py", line 2, in <module>
import json

This line is a giveaway: you have named your script "json", but you are trying to import the builtin module called "json", since your script is in the current directory, it comes first in sys.path, and so that's the module that gets imported.

You need to rename your script to something else, preferrably not a standard python module.

AttributeError: 'module' object has no attribute 'loads' while parsing json in python

The problem is that you're using Python 2.5.x, which doesn't have the json module. If possible, I recommend upgrading to Python 2.7.x, as 2.5.x is badly outdated.

If you need to stick with Python 2.5.x, you'll have to use the simplejson module (see here). This code will work for 2.5.x as well as newer Python versions:

try:
import json
except ImportError:
import simplejson as json

Or if you're only using Python 2.5, just do:

import simplejson as json 

Python/Json AttributeError: partially initialized module 'json' has no attribute

When you name your script the name of the module you try to import, python tries to imports your script first, which results in the Error

AttributeError: 'str' object has no attribute 'loads', json.loads()

json.load takes in a file pointer, and you're passing in a string. You probably meant to use json.loads which takes in a string as its first parameter.

Secondly, when you import json, you should take care to not overwrite it, unless it's completely intentional: json = json.load(teststr) <-- Bad.
This overrides the module that you have just imported, making any future calls to the module actually function calls to the dict that was created.

To fix this, you can use another variable once loaded:

import json
teststr = '{"user": { "user_id": 2131, "name": "John", "gender": 0, "thumb_url": "sd", "money": 23, "cash": 2, "material": 5}}'
json_obj = json.loads(teststr)

OR you can change the module name you're importing

import json as JSON
teststr = '{"user": { "user_id": 2131, "name": "John", "gender": 0, "thumb_url": "sd", "money": 23, "cash": 2, "material": 5}}'
json = JSON.loads(teststr)

OR you can specifically import which functions you want to use from the module

from json import loads
teststr = '{"user": { "user_id": 2131, "name": "John", "gender": 0, "thumb_url": "sd", "money": 23, "cash": 2, "material": 5}}'
json = loads(teststr)

Dict has no attribute write() on json

You need to open new file to write

Here is the example:

import json

def get_json(path):
with open(path, 'r') as f:
return json.load(f)

def write_json(path, data):
with open(path, 'w', encoding='utf-8') as config_file:
json.dump(data, config_file, indent=4)

if __name__ == '__main__':
data = get_json('input.json')
write_json('output.json', data)

Take a look at line:

with open(path, 'w', encoding='utf-8') as config_file:


Related Topics



Leave a reply



Submit