user3145912
user3145912

Reputation: 131

Why does json.dumps change the casing of boolean value?

I've been using json.dumps() to create some templates but when using it, it changes the casing of my boolean values Here's how to replicate:

import json

test_obj = {'field': False}
print json.dumps(test_obj)
# prints {"field": false}

What's the reason for this?

Upvotes: 0

Views: 6679

Answers (2)

Chitharanjan Das
Chitharanjan Das

Reputation: 1333

json.dumps() converts a Python dictionary to a JSON object formatted as a string. According to the JSON specification, the two boolean literals in JSON are true and false. So, json.dumps() isn't changing the casing, it's only converting Python's boolean literals, True and False, to JSON's boolean literals, true and false, respectively.

If you do wish to get a string representation of the Python dictionary without converting to JSON, you could cast the dictionary to string using str()

Upvotes: 4

gonczor
gonczor

Reputation: 4146

JSON stands for JavaScript Object Notation and true is boolean representation in JS. Compare with these docs.

Upvotes: 0

Related Questions