Skip to main content

JSON with datetime support.

This module is a light shim around the existing standard library JSON moduile.

It encodes and decodes Python datetime objects as human-readable ISO8601 dates.

https://raw.githubusercontent.com/zeth/jsondt/master/iso8601.jpg

It is a drop-in replacement for standard library JSON, so you can just do:

>>> import jsondt as json

Then carry on with the standard library JSON documentation.

Currently, this module supports only Python 3.7 and above.

Backporting to older versions is possible if there is demand for it.

Simple example

Here we go:

>>> import jsondt as json
>>> from datetime import datetime
>>> myobject = {'ctime': datetime.now()}
>>> encoded = json.dumps(myobject)
>>> print(encoded)
'{"ctime": "2019-08-19T18:18:25.609815"}'
>>> decoded = json.loads(encoded)
>>> print(decoded)
{'ctime': datetime.datetime(2019, 8, 19, 18, 18, 25, 609815)}
>>> myobject == decoded
True

JavaScript compatible

One of the main use cases is to interoperate with JSON produced by JavaScript in the browser.

Spin up your Javascript console:

>>> const record = {'ctime': new Date()}
>>> const encoded = JSON.stringify(record);
>>> console.log(encoded)
{"ctime":"2019-08-19T17:25:03.547Z"}

Now get that JSON string to Python through your API call or post request (here’s one that we made earlier):

>>> encoded = '{"ctime":"2019-08-19T17:25:03.547Z"}'
>>> import jsondt as json
>>> json.loads(encoded)
{'ctime': datetime.datetime(2019, 8, 19, 17, 25, 3, 547000,
tzinfo=datetime.timezone.utc)}

Say Hello

I am interested in more common use cases for ISO 8601 dates.

If you have an ISO 8601 date in JSON produced by a well known application or library (in any language) and jsondt does not recognise it, please make an issue on GitHub and I will see if I can add support.

Control Mode

You probably don’t need to care about this bit.

There is a second mode that is semi-automatic instead of fully-automatic.

Imagine we have this strange object:

>>> strange = {'a_date': datetime.datetime(2019, 8, 19, 21, 32, 59, 169730),
...            'b_date': '2018-05-01T07:03:44.560600'}

If we dump it and load it back, it will load both dates as datetime objects.

>>> import jsondt as json
>>> encoded = json.dumps(strange)
>>> decoded = json.loads(encoded)
>>> decoded == strange
False

If however, you prefer the original object back, you can use control mode.

>>> import jsondt as json
>>> encoded = json.dumps(strange, control=True)
>>> decoded = json.loads(encoded, control=True)
>>> decoded == strange
True

The way it works, is that with control=True, it puts a control code (backslash D) at the start of the encoded date. Like so:

>>> print(encoded)
{"a_date": "\\D2019-08-19T21:32:59.169730",
 "b_date": "2018-05-01T07:03:44.560600"}

Thanks for reading

If you want more examples, read the supplied tests.

Release files for jsondt 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for jsondt 1.0.0
File Size Uploaded
jsondt-1.0.0.tar.gz 10.8 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for jsondt 1.0.0
File Interpreter ABI Platform
jsondt-1.0.0-py3.7.egg Legacy Egg format - - Details
jsondt-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 26.0 kB

Release files / jsondt-1.0.0.tar.gz

Download URL jsondt-1.0.0.tar.gz
Size 10.8 kB
Tags Source
SHA-256 checksum
How to use checksums
866b1fff9f7b2cfd89979790c0b2cc92d9be74861b2d9738430186bce9ad96cc
BLAKE2b-256 checksum
How to use checksums
113c027ee3781623962b7cfdc8f6da814312b5443897ae49b9ff047f708e2b8a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.1.0 requests-toolbelt/0.9.1 tqdm/4.34.0 CPython/3.7.3

Release files / jsondt-1.0.0-py3.7.egg

Download URL jsondt-1.0.0-py3.7.egg
Size 5.5 kB
Tags Egg
SHA-256 checksum
How to use checksums
4712727467b8d3b292f6e129de013169574e650fe949c5d62c77a5afd9659a20
BLAKE2b-256 checksum
How to use checksums
43c7d954b90b58e9f78b1544e388fab407f53621711befd1c3831cd1a120f316
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.1.0 requests-toolbelt/0.9.1 tqdm/4.34.0 CPython/3.7.3

Release files / jsondt-1.0.0-py3-none-any.whl

Download URL jsondt-1.0.0-py3-none-any.whl
Size 9.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
71ac895897c4d2da9471c1c900de31063b4034181f98e19c523e11b9eaa4d932
BLAKE2b-256 checksum
How to use checksums
3911b03dfd8f813a3e07d397fd9fcd615d667002eeac28a22a50f36a57ccf5ae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.1.0 requests-toolbelt/0.9.1 tqdm/4.34.0 CPython/3.7.3

Release history Release notifications | RSS feed

This release

1.0.0 This release

3 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page