0

I think it's my first time asking for help in here, so here we go.

When I try to serialize this nested dict:

{'deal_object': {'archived': False,
                 'archived_at': None,
                 'associated_objects': {'companies': [{'archived': False,
                                                       'createdAt': '2021-09-05T06:19:30.763Z',
                                                       'id': '6892382874',
                                                       'properties': {'createdate': '2021-09-05T06:19:30.763Z',
                                                                      'domain': 'thegoodplace.com',
                                                                      'hs_lastmodifieddate': '2021-09-17T14:09:31.483Z',
                                                                      'hs_object_id': '6892382874',
                                                                      'name': 'The '
                                                                              'Good '
                                                                              'Place'},
                                                       'updatedAt': '2021-09-17T14:09:31.483Z'}],
                                        'contacts': [{'archived': False,
                                                      'createdAt': '2021-09-05T06:12:54.613Z',
                                                      'id': '2251',
                                                      'properties': {'createdate': '2021-09-05T06:12:54.613Z',
                                                                     'email': 'cjanku2k@loc.gov',
                                                                     'firstname': 'Clementius',
                                                                     'hs_object_id': '2251',
                                                                     'lastmodifieddate': '2021-09-17T13:21:06.366Z',
                                                                     'lastname': 'Janku'},
                                                      'updatedAt': '2021-09-17T13:21:06.366Z'}],
                                        'line_items': [{'archived': False,
                                                        'createdAt': '2021-09-17T14:09:30.471Z',
                                                        'id': '1991651896',
                                                        'properties': {'amount': '766.000',
                                                                       'createdate': '2021-09-17T14:09:30.471Z',
                                                                       'hs_lastmodifieddate': '2021-09-17T14:09:30.471Z',
                                                                       'hs_object_id': '1991651896',
                                                                       'hs_product_id': '1233509421',
                                                                       'quantity': '1'},
                                                        'updatedAt': '2021-09-17T14:09:30.471Z'},
                                                       {'archived': False,
                                                        'createdAt': '2021-09-17T14:09:30.471Z',
                                                        'id': '1991651897',
                                                        'properties': {'amount': '515.000',
                                                                       'createdate': '2021-09-17T14:09:30.471Z',
                                                                       'hs_lastmodifieddate': '2021-09-17T14:09:30.471Z',
                                                                       'hs_object_id': '1991651897',
                                                                       'hs_product_id': '1233499680',
                                                                       'quantity': '1'},
                                                        'updatedAt': '2021-09-17T14:09:30.471Z'}],
                                        'quotes': [{'archived': False,
                                                    'createdAt': '2021-09-20T14:36:46.780Z',
                                                    'id': '35496096',
                                                    'properties': {'hs_createdate': '2021-09-20T14:36:46.780Z',
                                                                   'hs_expiration_date': '2021-12-20T04:59:59.999Z',
                                                                   'hs_lastmodifieddate': '2021-09-20T14:37:04.683Z',
                                                                   'hs_object_id': '35496096',
                                                                   'hs_public_url_key': 'Twac8MWsquCFwmClG',
                                                                   'hs_status': 'APPROVAL_NOT_NEEDED',
                                                                   'hs_title': 'Good '
                                                                               'Place '
                                                                               'frozen '
                                                                               'yogurt'},
                                                    'updatedAt': '2021-09-20T14:37:04.683Z'}],
                                        'tickets': [{'archived': False,
                                                     'createdAt': '2021-09-15T14:35:55.693Z',
                                                     'id': '569342037',
                                                     'properties': {'content': 'lorem',
                                                                    'createdate': '2021-09-15T14:35:55.693Z',
                                                                    'hs_lastmodifieddate': '2021-09-20T14:36:03.446Z',
                                                                    'hs_object_id': '569342037',
                                                                    'hs_pipeline': '0',
                                                                    'hs_pipeline_stage': '3',
                                                                    'hs_ticket_category': None,
                                                                    'hs_ticket_priority': 'HIGH',
                                                                    'subject': 'Test '
                                                                               'ticket'},
                                                     'updatedAt': '2021-09-20T14:36:03.446Z'}]},
                 'associations': None,
                 'created_at': datetime.datetime(2021, 9, 5, 6, 20, 31, 767000, tzinfo=tzutc()),
                 'id': '6141872474',
                 'properties': {'createdate': '2021-09-05T06:20:31.767Z',
                                'dealtype': 'existingbusiness',
                                'hs_lastmodifieddate': '2021-09-17T14:09:31.251Z',
                                'hs_object_id': '6141872474',
                                'hs_priority': 'medium'},
                 'updated_at': datetime.datetime(2021, 9, 17, 14, 9, 31, 251000, tzinfo=tzutc())}}

with:

json.dumps(data) 

I get this error:

TypeError: Object of type datetime is not JSON serializable

So obviously I start by googling this error message and I do find many people suggesting same solution:

data_to_json = json.dumps(data, default=str)

However, when I use this solution it adds to the JSON alot of stuff that doesn't belong there. Let me show:

('{\n'
 '    "deal_object": {\n'
 '        "archived": false,\n'
 '        "archived_at": null,\n'
 '        "associated_objects": {\n'
 '            "companies": [\n'
 '                {\n'
 '                    "archived": false,\n'
 '                    "createdAt": "2021-09-05T06:19:30.763Z",\n'
 '                    "id": "6892382874",\n'
 '                    "properties": {\n'
 '                        "createdate": "2021-09-05T06:19:30.763Z",\n'
 '                        "domain": "thegoodplace.com",\n'
 '                        "hs_lastmodifieddate": "2021-09-17T14:09:31.483Z",\n'
 '                        "hs_object_id": "6892382874",\n'
 '                        "name": "The Good Place"\n'
 '                    },\n'
 '                    "updatedAt": "2021-09-17T14:09:31.483Z"\n'
 '                }\n'
 '            ],\n'
 '            "contacts": [\n'
 '                {\n'
 '                    "archived": false,\n'
 '                    "createdAt": "2021-09-05T06:12:54.613Z",\n'
 '                    "id": "2251",\n'
 '                    "properties": {\n'
 '                        "createdate": "2021-09-05T06:12:54.613Z",\n'
 '                        "email": "cjanku2k@loc.gov",\n'
 '                        "firstname": "Clementius",\n'
 '                        "hs_object_id": "2251",\n'
 '                        "lastmodifieddate": "2021-09-17T13:21:06.366Z",\n'
 '                        "lastname": "Janku"\n'
 '                    },\n'
 '                    "updatedAt": "2021-09-17T13:21:06.366Z"\n'
 '                }\n'
 '            ],\n'
 '            "line_items": [\n'
 '                {\n'
 '                    "archived": false,\n'
 '                    "createdAt": "2021-09-17T14:09:30.471Z",\n'
 '                    "id": "1991651896",\n'
 '                    "properties": {\n'
 '                        "amount": "766.000",\n'
 '                        "createdate": "2021-09-17T14:09:30.471Z",\n'
 '                        "hs_lastmodifieddate": "2021-09-17T14:09:30.471Z",\n'
 '                        "hs_object_id": "1991651896",\n'
 '                        "hs_product_id": "1233509421",\n'
 '                        "quantity": "1"\n'
 '                    },\n'
 '                    "updatedAt": "2021-09-17T14:09:30.471Z"\n'
 '                },\n'
 '                {\n'
 '                    "archived": false,\n'
 '                    "createdAt": "2021-09-17T14:09:30.471Z",\n'
 '                    "id": "1991651897",\n'
 '                    "properties": {\n'
 '                        "amount": "515.000",\n'
 '                        "createdate": "2021-09-17T14:09:30.471Z",\n'
 '                        "hs_lastmodifieddate": "2021-09-17T14:09:30.471Z",\n'
 '                        "hs_object_id": "1991651897",\n'
 '                        "hs_product_id": "1233499680",\n'
 '                        "quantity": "1"\n'
 '                    },\n'
 '                    "updatedAt": "2021-09-17T14:09:30.471Z"\n'
 '                }\n'
 '            ],\n'
 '            "quotes": [\n'
 '                {\n'
 '                    "archived": false,\n'
 '                    "createdAt": "2021-09-20T14:36:46.780Z",\n'
 '                    "id": "35496096",\n'
 '                    "properties": {\n'
 '                        "hs_createdate": "2021-09-20T14:36:46.780Z",\n'
 '                        "hs_expiration_date": "2021-12-20T04:59:59.999Z",\n'
 '                        "hs_lastmodifieddate": "2021-09-20T14:37:04.683Z",\n'
 '                        "hs_object_id": "35496096",\n'
 '                        "hs_public_url_key": "Twac8MWsquCFwmClG",\n'
 '                        "hs_status": "APPROVAL_NOT_NEEDED",\n'
 '                        "hs_title": "Good Place frozen yogurt"\n'
 '                    },\n'
 '                    "updatedAt": "2021-09-20T14:37:04.683Z"\n'
 '                }\n'
 '            ],\n'
 '            "tickets": [\n'
 '                {\n'
 '                    "archived": false,\n'
 '                    "createdAt": "2021-09-15T14:35:55.693Z",\n'
 '                    "id": "569342037",\n'
 '                    "properties": {\n'
 '                        "content": "lorem",\n'
 '                        "createdate": "2021-09-15T14:35:55.693Z",\n'
 '                        "hs_lastmodifieddate": "2021-09-20T14:36:03.446Z",\n'
 '                        "hs_object_id": "569342037",\n'
 '                        "hs_pipeline": "0",\n'
 '                        "hs_pipeline_stage": "3",\n'
 '                        "hs_ticket_category": null,\n'
 '                        "hs_ticket_priority": "HIGH",\n'
 '                        "subject": "Test ticket"\n'
 '                    },\n'
 '                    "updatedAt": "2021-09-20T14:36:03.446Z"\n'
 '                }\n'
 '            ]\n'
 '        },\n'
 '        "associations": null,\n'
 '        "created_at": "2021-09-05 06:20:31.767000+00:00",\n'
 '        "id": "6141872474",\n'
 '        "properties": {\n'
 '            "createdate": "2021-09-05T06:20:31.767Z",\n'
 '            "dealtype": "existingbusiness",\n'
 '            "hs_lastmodifieddate": "2021-09-17T14:09:31.251Z",\n'
 '            "hs_object_id": "6141872474",\n'
 '            "hs_priority": "medium"\n'
 '        },\n'
 '        "updated_at": "2021-09-17 14:09:31.251000+00:00"\n'
 '    }\n'
 '}')

So how I can get the JSON without all the

\n' and '

This is my first project in Python

So all help is appreciated!

ps: data used in this example is just mockup data from sandbox account.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Ron
  • 1
  • 1
    see https://stackoverflow.com/questions/11875770/how-to-overcome-datetime-datetime-not-json-serializable – balderman Sep 23 '21 at 14:39
  • the issue with the characters is due to the way you're printing it out. As you don't include this part we cannot answer your question in that regard. If I do a simple `print(data_to_json)` it prints it correctly for me. – Edo Akse Sep 23 '21 at 15:08
  • Oho wow.. You're right Edo Akse I used pprint() instead of print() and that was the problem. – Ron Sep 23 '21 at 17:18
  • yep... that *possibly* might have been crucial info to have – rv.kvetch Sep 23 '21 at 18:26

0 Answers0