Skip to content

Instantly share code, notes, and snippets.

@vojtatranta
Created May 27, 2015 07:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vojtatranta/474949cb54152d6ec61a to your computer and use it in GitHub Desktop.
Save vojtatranta/474949cb54152d6ec61a to your computer and use it in GitHub Desktop.
anything to JSON in python
def jsonize(self, data_dict):
dict_string = {}
for key, value in data_dict.items():
if isinstance(value, QuerySet):
dict_string[key] = serializers.serialize('json', value)
else:
dict_string[key] = json.dumps(value)
json_str = '{'
for key, value in dict_string.items():
json_str += '"%s":%s,' % (key, value)
json_str = json_str[:-1] + '}'
return json_str
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment