0

i have done following things

def handle_json(obj):
     # i want to make sure control goes here
     print(obj)
     return obj

def test_json():
     data= {datetime.date(2018, 12, 1): 160000.0,
             datetime.date(2019, 2, 1): 240000.0,
             datetime.date(2020, 9, 1): 1360000.0,
             datetime.date(2019, 3, 1): 280000.0}
     print(json.dumps(data, default=handle_json))

When i run test_json(), why i am not getting print() from handle_json() on console?

Gahan
  • 4,075
  • 4
  • 24
  • 44
108
  • 119
  • 2
  • 11
  • what are you thinking by using default??? default is used to specify separator and for the data you can just call the function – Gahan Apr 20 '18 at 05:47
  • you need to also convert DateTime object to string, if you plan to use json.dumps look here https://stackoverflow.com/questions/12734517/json-dumping-a-dict-gives-typeerror-keys-must-be-a-string?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Jibin Mathews Apr 20 '18 at 05:51
  • @JibinMathews yes this what i want to do. by getting that object into function and modify it. but i want to write a common method or class so that it becomes reusable – 108 Apr 20 '18 at 06:01
  • @AKS https://stackoverflow.com/questions/3768895/how-to-make-a-class-json-serializable?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa try a custom encoder. – Jibin Mathews Apr 20 '18 at 06:05

0 Answers0