Skip to main content

JSONField

This class provides a specialized field for handling JSON-encoded data, extending the functionality of a standard character field. It automatically manages the conversion between JSON strings and Python data structures like lists or dictionaries using customizable encoders and decoders. The field also includes built-in validation to ensure input data conforms to valid JSON syntax.

Attributes

AttributeTypeDescription
default_error_messagesdict = {"invalid": _("Enter a valid JSON.")}A dictionary containing the error message templates used by the field, specifically defining the 'invalid' message for malformed JSON input.
widget[Widget](../widgets/widget.md?sid=django_forms_widgets_widget) = TextareaThe form widget class used to render the field in the UI, defaulting to a multi-line text input area.

Constructor

Signature

def JSONField(
encoder: class = None,
decoder: class = None,
**kwargs: dict
)

Parameters

NameTypeDescription
encoderclass = NoneA custom JSON encoder class to use for serialization.
decoderclass = NoneA custom JSON decoder class to use for deserialization.
**kwargsdictAdditional keyword arguments passed to the parent CharField constructor.

Methods


to_python()

@classmethod
def to_python(
value: string|object
) - > object

Converts the input value into a Python object by parsing it as JSON. This method handles raw strings, existing Python structures, and ensures invalid JSON strings raise a ValidationError.

Parameters

NameTypeDescription
value`stringobject`

Returns

TypeDescription
objectThe deserialized Python representation of the JSON data, or a JSONString wrapper for string values.

bound_data()

@classmethod
def bound_data(
data: string,
initial: object
) - > object

Returns the Python representation of data bound to the form field, handling potential decoding errors gracefully. It returns an InvalidJSONInput marker if the data cannot be parsed.

Parameters

NameTypeDescription
datastringThe raw data submitted via the form for this field.
initialobjectThe initial value provided to the field when the form was instantiated.

Returns

TypeDescription
objectThe decoded Python object or an InvalidJSONInput instance if parsing fails.

prepare_value()

@classmethod
def prepare_value(
value: object
) - > string

Serializes the Python object into a JSON-formatted string suitable for display in a form widget. It preserves non-ASCII characters and handles previously invalid input markers.

Parameters

NameTypeDescription
valueobjectThe Python object or data structure to be serialized for the widget.

Returns

TypeDescription
stringA JSON-encoded string representation of the value.

has_changed()

@classmethod
def has_changed(
initial: object,
data: string|object
) - > boolean

Determines if the field data has changed by comparing the initial and current values as serialized JSON. This ensures that key ordering and type differences (like 1 vs True) are handled correctly during comparison.

Parameters

NameTypeDescription
initialobjectThe original value of the field before user modification.
data`stringobject`

Returns

TypeDescription
booleanTrue if the data has changed from its initial state, False otherwise.