If your keys are not nested, then it's just a simple operation of str(value).lower(), otherwise you'd have to do it recursively.
# this should work for basic data types
def convert_booleans(value):
if isinstance(value, dict):
return {key: convert_booleans(val) for key, val in value.items()}
elif isinstance(value, bool):
return str(value).lower()
elif hasattr(value, '__iter__'):
return map(convert_booleans, value)
return value
converted_data = convert_booleans(data)
print(json.dumps(converted_data))
With that said, it is not an ideal thing to do. JSON object has a special meaning for the boolean value, and it shouldn't be treated as a string. You had better try to convince the client of your api if possible. Most likely, the client is processing the result data as a string and not json, and hence finding it difficult to parse the data.
{'key1': 'true' if True else 'false'}? Or some potentially deep nested structure with arbitrary values?