Thursday, 11 August 2022

python, django saving resp.json() dict or dict to DB, then retrieve from DB to cast to JSON

when saving dict to DB in django, dict is automtically cast to string.

When saving json dict convereted from  python requests library response object.json(), there will be some issues :(as this json dict is converted by requests library response build in function .json())

resp=requests.post()

dict=resp.json()

The problem are 

1) "true" will be converted to True

2) null will be converted to None

3) additional quotes will be auto added


3) can be solved using str.replace

https://stackoverflow.com/questions/988228/convert-a-string-representation-of-a-dictionary-to-a-dictionary

import json
s = "{'muffin' : 'lolz', 'foo' : 'kitty'}"
json_acceptable_string = s.replace("'", "\"")

However, 1), 2) will cause issues if using json.loads to convert them into dict again,
as True and None are not supported by Json, and even directrly return this string to browser
will be an inorrect JSON
# produces error
d = json.loads(json_acceptable_string)
# # produces error in browser
HttpResponse(content=json_acceptable_string )

solution to 1) and 2)
https://www.delftstack.com/howto/python/check-if-variable-is-none-python/
https://stackoverflow.com/questions/11700705/how-to-recursively-replace-character-in-keys-of-a-nested-dictionary
is to create a recursive function to change None to '', and True to string 'true' before saving to db
def format_instance(instance):
    if isinstance(instance, (str,int,float,bool,type(None))):
        if type(instance) is bool:
            instance = str(instance).lower()
        if instance is None:
            instance = ''
        return instance

    if isinstance(instance, (list, set, tuple)):
        for index in range(len(instance)):
            instance[index]=format_instance(instance[index])

    if isinstance(instance,dict) :
        for key in instance:
           instance[key]= format_instance(instance[key])

    return instance

No comments:

Post a Comment