Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python json boolean to lowercase string

Is there a best-practice for outputting Booleans in Python? I'm generating some JSON (via Django templates), and by default all the Boolean values are output with leading character in uppercase, contrary to the JSON standard (ie, "True" as opposed to "true").

Currently, I format each Boolean string using str.lower(), but is there a better way?

like image 207
btk Avatar asked Sep 07 '11 14:09

btk


People also ask

How do you convert a Boolean to a string in Python?

Convert bool to string: str() You can convert True and False to strings 'True' and 'False' with str() . Non-empty strings are considered True , so if you convert False to strings with str() and then back to bool type with bool() , it will be True .


1 Answers

Well, then serialise to JSON using json, not some custom thingy.

import json
print json.dumps({'foo': True}) # => {"foo": true}
like image 165
Cat Plus Plus Avatar answered Sep 28 '22 06:09

Cat Plus Plus