JSON Field
Provides mechanisms for storing JSON data in Drupal fields with various field types, widgets, and formatters for editing and displaying JSON content.
json_field
Install
composer require 'drupal/json_field:8.x-1.7'
composer require 'drupal/json_field:8.x-1.3'
Overview
JSON Field is a comprehensive module that enables storage of JSON data within Drupal entity fields. It provides three distinct field types optimized for different database backends and use cases: a text-based storage option compatible with all databases, and two native JSON storage options that leverage database-specific JSON column types for MySQL, PostgreSQL, MariaDB, and SQLite.
The module includes validation to ensure only valid JSON can be saved, multiple display formatters for viewing JSON data, and optional enhanced editing capabilities through the JSON Editor WYSIWYG widget. It integrates seamlessly with Views for displaying JSON data in listings, supports the Feeds module for importing JSON content, and works with the Diff module for comparing JSON field revisions.
For JSON:API integration, the module can be configured with JSON:API Extras to output raw JSON instead of escaped strings, making it ideal for headless Drupal implementations and API-first architectures.
Features
- Three JSON field types with different storage backends: text-based (VARCHAR/TEXT), native JSON, and JSONB/JSON for database-optimized storage
- Automatic JSON validation to ensure only valid JSON data can be saved to fields
- Plain textarea widget for basic JSON editing with optional JSON Editor WYSIWYG widget for enhanced editing experience
- Multiple display formatters including plain text with optional JSONView library and human-readable Pretty formatter
- Views integration with special JSON data field handler for REST exports
- Support for JSON Schema validation when using the JSON Editor widget submodule
- Integration with Feeds module for importing JSON data from external sources
- Integration with Diff module for comparing JSON field revisions between entity versions
- Custom serialization normalizer for proper JSON output in REST and JSON:API contexts
- Configurable field storage size options (255 characters to 1GB) for text-based storage
Use Cases
Storing API Response Data
Store complete API responses from external services in JSON fields for caching, auditing, or further processing. The native JSON field types enable database-level querying of the stored data on supported databases.
Flexible Content Attributes
Add dynamic, schema-free attributes to content types without creating additional fields. Store product specifications, event metadata, or user preferences as JSON objects that can vary between entities.
Configuration Storage for Custom Functionality
Store complex configuration data for custom modules or integrations. Use JSON Schema validation in the editor widget to ensure configuration adheres to expected structure.
Headless Drupal API Data
Provide structured JSON data through Drupal's REST or JSON:API endpoints for decoupled frontend applications. The custom normalizer and JSON:API Extras integration ensure proper JSON output.
Data Migration Staging
Temporarily store complex data during migrations before processing into final Drupal structures. The JSON fields can hold nested data that would require multiple related entities in the final schema.
Form Builder Data Storage
Store form definitions, survey questions, or quiz data as JSON. The tree and form editor modes make it easy for content editors to manage nested data structures without writing raw JSON.
Tips
- Use JSON (text) field type for maximum database compatibility; use native JSON types only when you need database-level JSON querying
- Configure the 'Maximum space allowed' setting appropriately to avoid using more database resources than necessary
- The Pretty formatter requires no external libraries and is ideal for simple JSON display needs
- Enable JSON Schema validation in the editor widget to enforce data structure requirements for complex use cases
- MariaDB stores JSON in LONGTEXT columns but still supports JSON query operations against the data
- When using with Views REST exports, select the field with '(data)' suffix to output raw JSON instead of strings
Technical Details
Troubleshooting 6
Install JSON:API Extras module, override the resource type at admin/config/services/jsonapi/resource_types, and select 'JSON Field' as the enhancer for the JSON field.
Ensure the jquery-jsonview library is installed at /libraries/jquery-jsonview/dist/ with both jquery.jsonview.js and jquery.jsonview.css files present.
Enable the JSON Field Widget submodule, install the jsoneditor library at /libraries/jsoneditor/dist/, and change the field widget to 'JSON-specific WYSIWYG editor' in the form display settings.
Verify your database version meets the minimum requirements: MySQL 5.7.8+, MariaDB 10.2.7+, PostgreSQL 9.2+, or SQLite 3.26+. For native JSON field types, the database must support JSON columns.
This was fixed in version 8.x-1.6. Update to the latest version to resolve schema validation issues with empty non-required fields.
Drupal core's database export script doesn't fully support JSON field types. Track progress on core issues #3343634 (META), #3143512 (MySQL/MariaDB), #2472709 (PostgreSQL), and #3325871 (SQLite).
Security Notes 4
- JSON data is validated before saving to ensure only valid JSON can be stored
- The module uses Drupal's serialization service for JSON encoding/decoding, following security best practices
- JSON Schema validation (when enabled) provides an additional layer of data integrity verification
- Field-level permissions are controlled through standard Drupal field access mechanisms