PHPackages                             fiedsch/contao-jsonwidget - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. fiedsch/contao-jsonwidget

ActiveContao-bundle[Parsing &amp; Serialization](/categories/parsing)

fiedsch/contao-jsonwidget
=========================

widget for Contao Open Source CMS backend forms

0.8.4(3mo ago)624.9k↓22.7%11MITPHPPHP ^8.1

Since Feb 1Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/fiedsch/contao-jsonwidget)[ Packagist](https://packagist.org/packages/fiedsch/contao-jsonwidget)[ RSS](/packages/fiedsch-contao-jsonwidget/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (3)Versions (25)Used By (1)

JSON and YAML Widgets for Contao
================================

[](#json-and-yaml-widgets-for-contao)

The `jsonWidget` can be used in DCA files to create a text field that contains a JSON string. While saving it will be checked if that the string is valid JSON. The widget displays the JSON string with `JSON_PRETTY_PRINT` so that checking/finding errors is easier for the user.

The `yamlWidget` is mostly the same, except that it uses the YAML format.

Example: extending Members
--------------------------

[](#example-extending-members)

### DCA

[](#dca)

```
$GLOBALS['TL_DCA']['tl_member']['fields']['json_data'] = [
   'inputType' => 'jsonWidget',
   'label'     => &$GLOBALS['TL_LANG']['tl_member']['json_data'],
   'eval'      => ['tl_class' => 'long', 'decodeEntities' => true],
   'sql'       => "blob NULL",
 ];

 // Add json_data to $GLOBALS['TL_DCA']['tl_member']['palettes']['default']
 // where ever you like
```

Other valid options in `eval` are the same as for `textarea`s (as `WidgetJSON` extends `TextArea`), except that setting `rte` will be ignored because the editors provided do not make sense here.

### How to use the JSON data?

[](#how-to-use-the-json-data)

Extend `tl_member` as in the above example. Then create an `ExtendedMemberModel` that extends Contao's `MemberModel`. In the magic methodd `__set()` and `_get` you can intercept the "fields" stored in `json_data`. The `Fiedsch\JsonWidgetBundle\JsonGetterSetterTrait` takes care of that:

```
// models/ExtendedMemberModel.php
namespace Contao;

use Fiedsch\JsonWidgetBundle\Traits\JsonGetterSetterTrait;

class ExtendedMemberModel extends MemberModel
{
    // let __set() and __get() take care of the JSON or YAML data (both at the same time will not work!)
    use JsonGetterSetterTrait;
    // or (see above!)
    use YamlGetterSetterTrait;

  /**
    * The column name we selected for the `jsonWidget` in the example above
    * @var string
    */
    protected static $strJsonColumn = 'my_json_data_column';

    /**
      * Same thing for the `yamlWidget`
      * @var string
      */
    protected static $strYamlColumn = 'my_yaml_data_column';

}
```

```
// config/config.php
$GLOBALS['TL_MODELS']['tl_member'] = 'Contao\ExtendedMemberModel';
```

### Using the Model

[](#using-the-model)

```
$member = \ExtendedMemberModel::findById(42);

// access fields columns created by contao's default DCA
printf("read member %s %s\n", $member->firstname, $member->lastname);

// access a field stored in our JSON data column
printf("transparently accessing a field from the JSON data ... '%s'\n", $member->whatever);

// set values and store in database
$member->a_key_for_a_scalar_value = "fourtytwo";
$member->key_for_an_array = ['an','array','containing','some','strings'];
$member->save(); // Note that saving will lose comments in your YAML-data
                 // as Symfony\Component\Yaml\Yaml will not save them
```

### YAML-Syntax Highlighting with ACE

[](#yaml-syntax-highlighting-with-ace)

Set

```
'eval' => [ /* ... , */ 'rte'=>'ace|yaml'],
```

in your field's DCA definitions.

Quick and dirty way: add desired CSS-Rules like e.g.

```
.ace_comment {
  color: red !important;
}
```

to your `be_ace.html5` (which you create it if you do not yet have a custom version). OR: use a custom backend style and add the CSS rules there.

### Set JSON (or JAML) Data

[](#set-json-or-jaml-data)

If you want to set all the JSON (or YAML) data at once you cannot use

```
# In this example we assume $strJsonColumn = 'json_data';
$data = [ 'foo' => 1, 'bar' => 'baz ];
$model->json_data = $data; # will throw an exception
```

To do this you have to use

```
$data = [ 'foo' => 1, 'bar' => 'baz ];
$model->setJsonColumnData(array $data);
# or
# $model->setYamlColumnData(array $data);
```

Note that this way previously set JSON (or YAML) data is overwritten.

Also note that the behaviour of `setJsonColumnData([])` (when setting an empty data array) changed in version `0.5.0`. Previously it created `[]` where it now creates `{}` in the respective database column.

In version `0.7.0` we slightly changed this again to achieve the original goal: The storage of empty arrays as object (`{}`) is only enforced on the top level of the data. In lower levels, empty arrays will be stored as arrays (`[]`) not as "empty" objects (`{}`): compare `{ "foo": [] }` (versions &gt;= 0.7.0) to `{ "foo": {} }` (versions &gt;=0.5.0 and &lt;0.7.0).

### Additional note

[](#additional-note)

When using Contao 5.7 or newer, you might not need this bundle. See [this PR](https://github.com/contao/contao/pull/8838) to learn, what the Contao native alternative is and how it works.

###  Health Score

53

—

FairBetter than 97% of packages

Maintenance81

Actively maintained with recent releases

Popularity32

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity72

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~126 days

Recently: every ~209 days

Total

24

Last Release

117d ago

PHP version history (2 changes)0.0.1PHP &gt;=7.1

0.6.0PHP ^8.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/5047601?v=4)[Andreas Fieger](/maintainers/fiedsch)[@fiedsch](https://github.com/fiedsch)

---

Top Contributors

[![fiedsch](https://avatars.githubusercontent.com/u/5047601?v=4)](https://github.com/fiedsch "fiedsch (52 commits)")

---

Tags

contaodcajson-datajsoncontaowidget

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/fiedsch-contao-jsonwidget/health.svg)

```
[![Health](https://phpackages.com/badges/fiedsch-contao-jsonwidget/health.svg)](https://phpackages.com/packages/fiedsch-contao-jsonwidget)
```

###  Alternatives

[kdn/yii2-json-editor

JSON editor widget (josdejong/jsoneditor) for Yii 2.

22570.0k3](/packages/kdn-yii2-json-editor)[wa72/jsonrpc-bundle

JSON-RPC server for Symfony: exposes services registered in the service container as JSON-RPC webservices

3164.1k](/packages/wa72-jsonrpc-bundle)[thunderer/serializard

Flexible serializer

2767.3k1](/packages/thunderer-serializard)[opensoft/simple-serializer

Simple Serializer

1914.2k1](/packages/opensoft-simple-serializer)[phppkg/config

Config manage, load, get. Supports INI,JSON,YAML,NEON,PHP format file

133.5k](/packages/phppkg-config)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
