Skip to main content
https://github.com/rpkilby/jsonfield/actions/workflows/main.yml/badge.svg https://codecov.io/gh/rpkilby/jsonfield/graph/badge.svg https://img.shields.io/pypi/l/jsonfield.svg https://img.shields.io/pypi/v/jsonfield.svg https://img.shields.io/pypi/pyversions/jsonfield.svg

jsonfield is a reusable model field that allows you to store validated JSON, automatically handling serialization to and from the database. To use, add jsonfield.JSONField to one of your models.

Deprecation & Migration to Django’s native JSONField

Django 3.1 introduced a native JSONField that supports all database backends. As such, this package is considered deprecated and will be archived in the future. Existing projects should migrate to Django’s implemenation.

Migrating from jsonfield.JSONField to models.JSONField should generally be straightforward. After swapping field classes, python manage.py migrate will generate AlterField operations that should correctly migrate your field data. However, if this does not work for your case, you will instead need to create a data migration. The process will roughly look like:

  • Rename <field> to old_<field>, create migration.

  • Add a nullable <field> = models.JSONField(null=True, ...), create migration.

  • Create an empty migration file, add RunPython operation that reserializes the old_<field> data into the new <field>.

  • Update <field> to not nullable, delete old_<field>, create migration.

  • Manually combine the operations into a single migration file.

Examples can be found in the migration-example project.

Installation

pip install jsonfield

Usage

from django.db import models
from jsonfield import JSONField

class MyModel(models.Model):
    json = JSONField()

Querying

As stated above, JSONField is not intended to provide extended querying capabilities. That said, you may perform the same basic lookups provided by regular text fields (e.g., exact or regex lookups). Since values are stored as serialized JSON, it is highly recommended that you test your queries to ensure the expected results are returned.

Handling null values

A model field’s null argument typically controls whether null values may be stored in its column by setting a not-null constraint. However, because JSONField serializes its values (including nulls), this option instead controls how null values are persisted. If null=True, then nulls are not serialized and are stored as a null value in the database. If null=False, then the null is instead stored in its serialized form.

This in turn affects how null values may be queried. Both fields support exact matching:

MyModel.objects.filter(json=None)

However, if you want to use the isnull lookup, you must set null=True.

class MyModel(models.Model):
    json = JSONField(null=True)

MyModel.objects.filter(json__isnull=True)

Note that as JSONField.null does not prevent nulls from being stored, achieving this must instead be handled with a validator.

Advanced Usage

By default python deserializes json into dict objects. This behavior differs from the standard json behavior because python dicts do not have ordered keys. To overcome this limitation and keep the sort order of OrderedDict keys the deserialisation can be adjusted on model initialisation:

import collections

class MyModel(models.Model):
    json = JSONField(load_kwargs={'object_pairs_hook': collections.OrderedDict})

Other Fields

jsonfield.JSONCharField

Subclasses models.CharField instead of models.TextField.

Running the tests

The test suite requires tox.

$ pip install tox

Then, run the tox command, which will run all test jobs.

$ tox

Or, to test just one job (for example Django 5.2 on Python 3.13):

$ tox -e py313-django52

Release Process

  • Update changelog

  • Update package version in setup.py

  • Check supported versions in setup.py and readme

  • Create git tag for version

  • Upload release to PyPI test server

  • Upload release to official PyPI server

$ pip install -U pip setuptools wheel twine
$ rm -rf dist/ build/
$ python setup.py sdist bdist_wheel
$ twine upload -r test dist/*
$ twine upload dist/*

Changes

Take a look at the changelog.

Release files for jsonfield 3.2.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 jsonfield 3.2.0
File Size Uploaded
jsonfield-3.2.0.tar.gz 17.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for jsonfield 3.2.0
File Interpreter ABI Platform
jsonfield-3.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 25.5 kB

Release files / jsonfield-3.2.0.tar.gz

Download URL jsonfield-3.2.0.tar.gz
Size 17.2 kB
Tags Source
SHA-256 checksum
How to use checksums
ca53871bc3308ae4f4cddc3b4f99ed5c6fc6abb1832fbfb499bc6da566c70e4a
BLAKE2b-256 checksum
How to use checksums
fae9537e105246dba81d898853dbbe17eb3edd23d47a35074b99fd4add6f1662
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.5

Release files / jsonfield-3.2.0-py3-none-any.whl

Download URL jsonfield-3.2.0-py3-none-any.whl
Size 8.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ca4f6bf89c819f293e77074d613c0021e3c4e8521be95c73d03caecb4372e1ee
BLAKE2b-256 checksum
How to use checksums
0a222e08e7b957f50e5eceefde018ce9ee88aceb5126231128d9c1cb8167c1c8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.5

Release history Release notifications | RSS feed

This release

3.2.0 This release

2 release files

3.1.0

2 release files

3.0.0

2 release files

2.1.1

2 release files

2.1.0

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.0.3

1 release file

1.0.2

1 release file

1.0.1

1 release file

1.0.0

1 release file

0.9.23

1 release file

0.9.22

1 release file

0.9.20

1 release file

0.9.19

1 release file

0.9.18

1 release file

0.9.17

1 release file

0.9.15

1 release file

0.9.14

1 release file

0.9.13

1 release file

0.9.12

1 release file

0.9.11

1 release file

0.9.10

1 release file

0.9.9

1 release file

0.9.8

1 release file

0.9.7

1 release file

0.9.6

1 release file

0.9.5

1 release file

0.9.4

1 release file

0.9.2

2 release files

0.9

1 release file

0.8

1 release file

0.7

2 release files

0.6

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