# CustomFields of type Json is saved only after json\_encode() in address update

**URL:** <https://forum.shopware.com/t/customfields-of-type-json-is-saved-only-after-json-encode-in-address-update/100855>\
**Category:** Shopware 6 (English)\
**Tags:** programming\
**Created:** [14. August 2023 um 10:05 UTC](https://forum.shopware.com/t/customfields-of-type-json-is-saved-only-after-json-encode-in-address-update/100855 "2023-08-14T10:05:00Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![filippo.vicari](https://avatars.discourse-cdn.com/v4/letter/f/9e8a1a/32.png) [@filippo.vicari](https://forum.shopware.com/u/filippo.vicari)\
**Post date:** [14. August 2023 um 10:05 UTC](https://forum.shopware.com/t/customfields-of-type-json-is-saved-only-after-json-encode-in-address-update/100855/1 "2023-08-14T10:05:00Z")

</div>

Hi eveyone I am facing an issue with something I want to achieve. The system version is 6.4.18.1

# My goal

I need to save customFields of type Json on address upsert this to store some data that will be retrieved afterwards.

# What i have already done

## plugin configuration

created custom fields (with „allowCustomerWrite“ = true) and custom fields set

```php
        $customerAddressPrefix = [
            'name' => 'customer_prefix',
            'config' => [
                'label' => [
                    'de-DE' => ' '
                ],
            ],
            "relations" => [
                [
                    "entityName" => CustomerAddressDefinition::ENTITY_NAME,
                ],
            ],
            'customFields' => [
                [
                    'name' => 'customer_address_prefix',
                    'type' => CustomFieldTypes::JSON,
                    "allowCustomerWrite" => true, 
                    'config' => [
                        'label' => [
                            'de-DE' => '',
                        ],
                        'helpText' => [
                            'de-DE' => '',
                        ],
                        'customFieldPosition' => 1,
                    ],
                ],
            ],
        ];

```

## UpsertAddressRouteDecorator

Added customFields to DataBag as follows

```php
        $address= $data->get("address");
        $address->set(
            "customFields",
            new RequestDataBag([
                "customer_address_prefix" =>
// THIS DID NOT WORK -> result in {"customer_address_prefix" => {}}
// [[
// 'type' => "aaaa",
// 'prefix' => "bbbb",
// 'id' => $genericAddress->get("id"),
// ]],
// NEITHER THIS WORKED -> result in {"customer_address_prefix" => {}}
// RequestDataBag([
// 'type' => "aaaa",
// 'prefix' => "bbbb",
// 'id' => $genericAddress->get("id"),
// ]),
                    json_encode([
                        'type' => $billingOrShipping,
                        'prefix' => $cleanPhonePrefix,
                        'id' => $genericAddress->get("id"),
                    ], JSON_THROW_ON_ERROR),
            ])
        );

```

# Actual problem

I cannot store customFields in Json format other that passing it with json\_encode(). But having so can lead to problem afterwards. Therefore I wanted to know whether anyone knows a way to store it (via DataBag) in Json format.

Thanks for you time and patience
