in Education by
I have a basic dict as follows: sample = {} sample['title'] = "String" sample['somedate'] = somedatetimehere When I try to do jsonify(sample) I get: TypeError: datetime.datetime(2012, 8, 8, 21, 46, 24, 862000) is not JSON serializable What can I do such that my dictionary sample can overcome the error above? Note: Though it may not be relevant, the dictionaries are generated from the retrieval of records out of MongoDB where when I print out str(sample['somedate']), the output is 2012-08-08 21:46:24.862000. Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
We have a simple solution based on a specific serializer that just converts datetime.datetime and datetime.date object to strings. from datetime import date, datetime def json_serial(obj): if isinstance(obj, (datetime, date)): return obj.isoformat() raise TypeError ("Type %s not serializable" % type(obj)) The above code just checks to find out if an object is of class datetime.datetime or datetime.date, and then uses .isoformat() to produce a serialized version of it. The code ends by raising an exception, to deal with the case it is called with a non-serializable type.

Related questions

0 votes
    This my my JSON: { "maps": [ { "id": "AT", "iscategorical": "0" }, { "id": "AT", "iscategorical": "0 ... the values, How can I do it? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    I have json file as follows: { "status":"UP", "diskSpace": {"status":"UP","total":63279460352," ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 16, 2022 in Education by JackTerrance
0 votes
    In java, how can I parse json text to get value of variables like post_id ? Here is my json text { "page": { ... ": "1234567890" } ] } Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    For example, assuming I can generate this page but won't be able to hard-code the article number, ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    I am using the following function to load a PlayList of Songs from 'PlayListJSON.aspx' but somethings seems ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    I want to parse a .json column through Power BI. I have imported the data directly from the server and ... simplified parsed columns. Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    Why can't we Python parse this JSON data?...
asked Jan 11, 2021 by JackTerrance
0 votes
    How do I convert a string variable jsonString into JSON object? Please help. {"phonetype":"N95","dog":"WP"} Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    How can we convert all elements of form to a JavaScript object?...
asked Jan 11, 2021 by JackTerrance
0 votes
    I try to merge some object into one array I did output by q = [["99","99","99"],["9" ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 1, 2022 in Education by JackTerrance
0 votes
    Is there a built-in method for converting a date to a datetime in Python, for example getting the datetime for the ... .month, d.day)? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    I want to covert a large complex date-time string into proper datetime fields in a database, Can someone tell me a ... can't use SQL. Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    I have two data frames with the closest matching DateTime index, sometimes matching. An object is to merge two of ... I achieve this? Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I'm having an issue currently with pulling from an API, getting a JSON dict, then flattening it, ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I'm having an issue currently with pulling from an API, getting a JSON dict, then flattening it, ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
...