I have a question about the difference between a plain Python dictionary object and what is returned as output of json.loads(str). As per checking type() on both the objects, it says dict. But i want to confirm this if json.load() actually returns a plain dict or a json object which is in dictionary format. My code is as below :
import json
my_dict={'key1': 'val1', 'key2': 'val2'}
my_json_string = json.dumps(my_dict)
resp_json=json.loads(my_json_string)
print(type(my_dict))
print(type(resp_json))
Output says : class 'dict' for both
But as menioned, I want to confirm if both are exactly the same thing, because I need to send back a JSON response and if both are exactly same, doesnt make sense to first convert it into a string using json.dumps() and then again do a json.loads() on it. I might as well return my dictionary object my_dict that I created. Hope my question is clear.
printonly shows the string representation of the type, which I guess might be misleading. Trytype(resp_json) == dictand you see that it's actually thatdicttype. - tobias_kresp_json is my_dictreturnsFalse) but I don't see why that should matter. - Stop harming Monicajson.loads()receives a string and iff it is a representation of a correct json object, it returns it's contents in a python dictionary, because they are similar/compatible types. - MTTI