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
5,048 sites
42
drupal.org
Drupal 9 Drupal 10 Drupal 11

インストール

Drupal 11, 10 v8.x-1.7
composer require 'drupal/json_field:8.x-1.7'
Drupal 9 v8.x-1.3
composer require 'drupal/json_field:8.x-1.3'

概要

JSON Fieldは、DrupalエンティティフィールドにJSONデータを保存できる包括的なモジュールです。異なるデータベースバックエンドとユースケースに最適化された3つの異なるフィールドタイプを提供します:すべてのデータベースと互換性のあるテキストベースのストレージオプション、およびMySQL、PostgreSQL、MariaDB、SQLite向けのデータベース固有のJSONカラムタイプを活用する2つのネイティブJSONストレージオプションです。

このモジュールには、有効なJSONのみが保存できることを確認するバリデーション、JSONデータを表示するための複数の表示フォーマッター、JSON Editor WYSIWYGウィジェットによるオプションの高度な編集機能が含まれています。ViewsとシームレスにインテグレーションしてJSONデータをリスト表示でき、Feedsモジュールを使用したJSONコンテンツのインポートをサポートし、Diffモジュールと連携してJSONフィールドのリビジョンを比較できます。

JSON:APIとのインテグレーションでは、JSON:API Extrasでエスケープされた文字列ではなく生のJSONを出力するように設定でき、ヘッドレスDrupal実装やAPIファーストアーキテクチャに最適です。

Features

  • 異なるストレージバックエンドを持つ3つのJSONフィールドタイプ:テキストベース(VARCHAR/TEXT)、ネイティブJSON、およびデータベース最適化ストレージ用のJSONB/JSON
  • 有効なJSONデータのみがフィールドに保存できることを保証する自動JSONバリデーション
  • 基本的なJSON編集用のプレーンテキストエリアウィジェットと、高度な編集体験のためのオプションのJSON Editor WYSIWYGウィジェット
  • オプションのJSONViewライブラリを使用したプレーンテキストと人間が読みやすいPrettyフォーマッターを含む複数の表示フォーマッター
  • RESTエクスポート用の特別なJSONデータフィールドハンドラーを備えたViewsインテグレーション
  • JSON Editorウィジェットサブモジュール使用時のJSON Schemaバリデーションサポート
  • 外部ソースからJSONデータをインポートするためのFeedsモジュールとのインテグレーション
  • エンティティバージョン間でJSONフィールドのリビジョンを比較するためのDiffモジュールとのインテグレーション
  • RESTおよびJSON:APIコンテキストでの適切なJSON出力のためのカスタムシリアライゼーションノーマライザー
  • テキストベースストレージ用の設定可能なフィールドストレージサイズオプション(255文字から1GB)

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
JSON fields show escaped strings in JSON:API responses

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.

JSONView library not displaying formatted output

Ensure the jquery-jsonview library is installed at /libraries/jquery-jsonview/dist/ with both jquery.jsonview.js and jquery.jsonview.css files present.

JSON Editor widget not appearing

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.

Database compatibility warnings in status report

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.

Validation error when saving empty optional JSON field

This was fixed in version 8.x-1.6. Update to the latest version to resolve schema validation issues with empty non-required fields.

Core database export issues with JSON columns

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