I am using drupal_http_request and drupal_json_decode to retrive and parse json from an external API in the feeds tamper by using the execute php code plugin. My code is as follows:

$json = "https://api.coursera.org/api/onDemandSpecializations.v1?ids=".$field;
$response = drupal_http_request($json);
$data = drupal_json_decode($response->data);
return ($data['elements'][0]['name']);

This returns a null value.

The json is as follows:

enter image description here

1 Answer 1


If I'm not mistaken {} indicates that it is an object and [] means that the variable is an array, therefor, I would expect $data->elements[0]->name would be the way to get the value in your return statement.

To improve your code, I would advise to use the empty() (or isset()) functions to check whether the elements in $data exists

  • Thanks.Doesn't drupal_json_decode convert the json object into a PHP array ?
    – user19466
    Feb 12, 2017 at 0:19
  • Yes, I think you're right. Could you print out what is stored in $data (using dpm())?
    – Watergate
    Feb 12, 2017 at 12:56
  • dpm($data) returns nothing, but $dpm($response) shows the corresponding json object. There must be some problem with the drupal_json_decode statement.
    – user19466
    Feb 13, 2017 at 15:42
  • 1
    I changed the code to json_decode, and it works now. Thanks for the help @Watergate.
    – user19466
    Feb 13, 2017 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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