Issue
I'm print a dictionary like JSON in Python2, I'm using print(json.dumps(request_dr.data['data']))
but right now the output from my console is:
{
"id": 711,
"username": "esteban@gtt",
"first_name": "esteban@gtt",
"last_name": "",
... Anothers fields
"passwordChangedOnce": "\u0001",
"ldapCheck": "\u0000"
}
So, How Can I convert passwordChangedOnce to true and ldapCheck to false, in order to I get some like this:
{
"id": 711,
"username": "esteban@gtt",
"first_name": "esteban@gtt",
"last_name": "",
... Anothers fields
"passwordChangedOnce": "true",
"ldapCheck": "false"
}
I have read another answers in Stack Overflow but I haven't had success. Thanks
Solution
There's nothing fancy or automatic about this -- if you want to do a search-and-replace on values, write code that does a search-and-replace on values.
def convertDict(d):
for (k, v) in d.items():
if v == '\x00': # this is the same string that json serializes as "\u0000"
d[k] = False
elif v == '\x01': # this is the same string that json serializes as "\u0001"
d[k] = True
return d
print(json.dumps(convertDict(request_dr.data['data'])))
Answered By - Charles Duffy
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.