json.dumps() converts a dictionary to str object, not a json(dict) object! So you have to load your str into a dict to use it by using json.loads() method
See json.dumps() as a save method and json.loads() as a retrieve method.
This is the code sample which might help you understand it more:
import json
r = {'is_claimed': 'True', 'rating': 3.5}
r = json.dumps(r)
loaded_r = json.loads(r)
loaded_r['rating'] #Output 3.5
type(r) #Output str
type(loaded_r) #Output dict
Answer from Iman Mirzadeh on Stack Overflowjson.dumps() converts a dictionary to str object, not a json(dict) object! So you have to load your str into a dict to use it by using json.loads() method
See json.dumps() as a save method and json.loads() as a retrieve method.
This is the code sample which might help you understand it more:
import json
r = {'is_claimed': 'True', 'rating': 3.5}
r = json.dumps(r)
loaded_r = json.loads(r)
loaded_r['rating'] #Output 3.5
type(r) #Output str
type(loaded_r) #Output dict
json.dumps() returns the JSON string representation of the python dict. See the docs
You can't do r['rating'] because r is a string, not a dict anymore
Perhaps you meant something like
r = {'is_claimed': 'True', 'rating': 3.5}
json = json.dumps(r) # note i gave it a different name
file.write(str(r['rating']))
How to serialize Python dict to JSON - Stack Overflow
python - How do I put a dictionary into JSON without the escape slash - Stack Overflow
convert list of dictionary to list of json objects
What's the real difference between a dict and a JSON object (coming from JS background)
Videos
The problem is, python doesn't know how to represent SomeObject
You can create a fallback like so:
import json
def dumper(obj):
try:
return obj.toJSON()
except:
return obj.__dict__
obj = {'someproperty': 0, 'anotherproperty': 'value', 'propertyobject': SomeObject(someproperty=0, anotherproperty=0)}
print json.dumps(obj, default=dumper, indent=2)
Python can serialize only the objects that is a built in data type. In your case, "SomeObject" is a User defined type that Python cannot serialize. If you try to serialize any data type which is not json serializable, you get a TypeError "TypeError: is not JSON serializable". So there should be an intermediate step that converts these non built in data types into Python built in serializable data structure (list, dict, number and string).
So let us convert your SomeObject into a python dictionary, since dictionary is the easiest way to represent your Object(as it has key/value pairs). You could just copy all your SomeObject instance attributes to a new dictionary and you are set! myDict = self.__dict__.copy() This myDict can now be the value of your "propertyobject".
After this step is when you convert dictionary to a string (JSON format, but it can be YAML, XML, CSV...) - for us it will be jsonObj = JSON.dumps(finalDict)
Last step is to write jsonObj string to a file on disk!
The reason is because you are dumping your JSON data twice. Once outside the function and another inside it. For reference:
>>> import json
>>> data = {'number':7, 'second_number':44}
# JSON dumped once, without `\`
>>> json.dumps(data)
'{"second_number": 44, "number": 7}'
# JSON dumped twice, with `\`
>>> json.dumps(json.dumps(data))
'"{\\"second_number\\": 44, \\"number\\": 7}"'
If you print the data dumped twice, you will see what you are getting currently, i.e:
>>> print json.dumps(json.dumps(data))
"{\"second_number\": 44, \"number\": 7}"
I had a slightly different problem that resulted in the same issue. My code had this:
requests.post('https://example.com/data', data=clinicListBody).text
When it should have had this
requests.post('https://example.com/data', data=clinicListBody).json()
.text was returning a string with strings inside it, which is why I was seeing escaped json in the saved file.
How to convert list of dictionary to list of json objects?
input = [{'a':'b'},{'c':'d'}]
expectedOutput = [{"a":"b"},{"c":"d"}]
I tried following but couldn't get expected result
json.dumps(input)gives string'[{"a":"b"},{"c":"d"}]'Iterate through dictionary and convert each dict to json object gives
['{"a":"b"}','{"c":"d"}']