Is it possible to convert each value pair of a dictionary into json and then put them back together as one JSON, I tried doing it as such but it failed and produced weird:
def toj(dict):
j=[]
for k,v in dict.items():
try:
j.append({json.dumps(k):json.dumps(v)})
except:
j.append({json.dumps(k):json.dumps(str(v))})
return j
why do I need it? -->
I need to pass around dictionary instance within my app which is currently using Flask as the web framework (It may change in the future).
from datetime import datetime
dic={'1':One, 2:'Two','list1':[1,2,3],'time_stamp':datetime.now()}
This dictionary is not json convertible as you all would know:
raise TypeError(f'Object of type {o.__class__.__name__} '
f'is not JSON serializable')
TypeError: Object of type datetime is not JSON serializable
Json.dumps acts on the entire object and not each key.So either the whole thing has to be sanitized.
Lucky for me, I I can solve this by converting the offending object into str. but this is not possible because
Full-disclosure---> my brain does not digest json easily
you can easily do this with default
keyword of json.dump
def handle_conversion(v):
if isinstace(v,datetime):
return str(v)
elif isinstance(v,some_other_thing_that_cannot_be_jsonified):
return "???" # a value that can be jsonified
return v
data = json.dumps(a_dict_with_datetimes,default=handle_conversion)
if you only need to pass serialized data between python you can use the pickle
module instead which serialized almost anything (but sometimes breaks depending on where you are decoding it)
here is a complete working example
import json
from datetime import datetime
class some_other_thing_that_cannot_be_jsonified:
pass
def handle_conversion(v):
if isinstance(v,datetime):
return str(v)
elif isinstance(v,some_other_thing_that_cannot_be_jsonified):
return "???" # a value that can be jsonified
return v
a_dict_with_datetimes = {
"dt":datetime.now(),"a":"hello","w":"world","L":[1,2,3]
}
data = json.dumps(a_dict_with_datetimes,default=handle_conversion)
print(data)
# {"dt": "2022-10-09 16:21:19.643080", "a": "hello", "w": "world", "q": [1, 2, 3]}
you could simplify it to also just always return str (it only calls default
if it could not jsonify the object), this will cause any object that cannot be jsonified to be converted to str
def handle_conversion(v):
# if isinstance(v,datetime):
# return str(v)
# elif isinstance(v,some_other_thing_that_cannot_be_jsonified):
# return "???" # a value that can be jsonified
return str(v)