Why do I get this error when I save my custom field? I created a custom field and this is its formElement() method.

public function formElement(FieldItemListInterface $items, $delta, array $element, array &amp;$form, FormStateInterface $form_state) {
  $value = isset($items[$delta]-&gt;value) ? $items[$delta]-&gt;value : '';
  $client = \Drupal::httpClient();
  $request = $client-&gt;get('http://localhost:8083/regionilist');
  $response = json_decode($request-&gt;getBody());

  $element += [
    '#type' =&gt; 'select',
    '#title' =&gt; $this-&gt;t('Select element'),
    '#options' =&gt; $response,
    //'#options' =&gt; [
    //  'Abruzzo' =&gt; $this-&gt;t('Abruzzo label'),
    //  'Lazio' =&gt; $this-&gt;t('Lazio label'),
    //  'Sicilia' =&gt; $this-&gt;t('Sicilia label'),
    //],
    '#suffix' =&gt; '&lt;div class="custom-field-widget"&gt;&lt;/div&gt;',
    '#attributes' =&gt; ['class' =&gt; ['edit-custom-field-widget']],
    '#attached' =&gt; [
      'library' =&gt; [
        'custom_field/my_js_custom_field',
      ],
    ],
  ];

  return ['value' =&gt; $element];
}


The Rest service output is this.

{
  "Abruzzo": "Abruzzo label",
  "Lazio": "Lazio label"
}


The output of json_decode() is the following one.

array (
  'Abruzzo' =&gt; 'Abruzzo label',
  'Lazio' =&gt; 'Lazio label',
)


When I add the field to a content type I get the following error.


  TypeError: Argument 1 passed to Drupal\Core\Form\OptGroup::flattenOptions() must be of the type array, object given, called in C:\xampp\htdocs\drupal8DPC\core\lib\Drupal\Core\Form\FormValidator.php on line 340 in Drupal\Core\Form\OptGroup::flattenOptions() (line 23 of core\lib\Drupal\Core\Form\OptGroup.php).
  Drupal\Core\Form\OptGroup::flattenOptions(Object) (Line: 340)
  Drupal\Core\Form\FormValidator->performRequiredValidation(Array, Object) (Line: 247)
   Drupal\Core\Form\FormValidator->doValidateForm(Array, Object) (Line: 239)
   Drupal\Core\Form\FormValidator->doValidateForm(Array, Object) (Line: 239)
   Drupal\Core\Form\FormValidator->doValidateForm(Array, Object) (Line: 239)
   Drupal\Core\Form\FormValidator->doValidateForm(Array, Object) (Line: 239)
   Drupal\Core\Form\FormValidator->doValidateForm(Array, Object, 'field_config_edit_form') (Line: 119)
  Drupal\Core\Form\FormValidator->validateForm('field_config_edit_form', Array, Object) (Line: 571)
  Drupal\Core\Form\FormBuilder->processForm('field_config_edit_form', Array, Object) (Line: 314)
  Drupal\Core\Form\FormBuilder->buildForm('field_config_edit_form', Object) (Line: 74)
  Drupal\Core\Controller\FormController->getContentResult(Object, Object) call_user_func_array(Array, Array) (Line: 123)
  Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber{closure}() (Line: 582)
  Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
  Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
  Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber{closure}() (Line: 151)
  Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 68)
  Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
  Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
  Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 99)
  Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 78)
  Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
  Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 38)
  Drupal\webprofiler\StackMiddleware\WebprofilerMiddleware->handle(Object, 1, 1) (Line: 50)
  Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
  Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 664)
  Drupal\Core\DrupalKernel->handle(Object) (Line: 19)


Error does not occur when I use the fixed code (commented in the code above) instead of the REST service.
Use the Json class in core, it’s set up to use associative arrays (rather than the default object from json_decode, which is causing your current issue):

$decoded = \Drupal\Component\Serialization\Json::decode(...);