I have JSON file with some id and value, and this need to be populated in form as select list:

$form['select_item'] = [
      '#type' => 'select',
      '#options' => $list,
      '#title' => $this->t('Choose'),
      '#required' => TRUE,

What is the best way to approach this. Can I somehow convert this JSON in array or something and use it in options?

I would like to have this values form JSON in separate file and to use it from there.

Any idea?


Drupal doesn't have a lot to help with this, it's really a general PHP task.

If you're determined to use the API for it, just load the file data in any way you like (fopen, file_get_contents, etc), and use Json::decode() to turn it into an array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.