1

I am using Drupal 7 Services to expose an API. The following is the code I am using to get some of the details and it is giving the response interms of arrays. But I don't want the response to be in Arrays. I want the response in JSON format. What should be the proper solution for this?

The code I am using is:

function get_gold_product_items() {

  $query = db_select('node','n');
  $query->Join('field_data_field_gold_price', 'gold_price', 'n.nid = gold_price.entity_id');

  $query->Join('field_data_field_gold_product_type', 'p3', 'n.nid = p3.entity_id');
  $query->Join('taxonomy_term_data', 'gold_product_type', 'p3.field_gold_product_type_tid = gold_product_type.tid');

  $query->Join('field_data_field_gold_price_range', 'p4', 'n.nid = p4.entity_id');
  $query->Join('taxonomy_term_data', 'gold_price_range', 'p4.field_gold_price_range_tid = gold_price_range.tid');

  $query->Join('field_data_field_gold_gender', 'p6', 'n.nid = p6.entity_id');
  $query->Join('taxonomy_term_data', 'gold_gender', 'p6.field_gold_gender_tid = gold_gender.tid');

  $query->Join('field_data_field_gold_wearing_style', 'p8', 'n.nid = p8.entity_id');
  $query->Join('taxonomy_term_data', 'gold_wearing_style', 'p8.field_gold_wearing_style_tid = gold_wearing_style.tid');

  $query->Join('field_data_field_gold_color', 'p10', 'n.nid = p10.entity_id');
  $query->Join('taxonomy_term_data', 'gold_color', 'p10.field_gold_color_tid = gold_color.tid');

  $query->LeftJoin('field_data_field_gold_design_type', 'p12', 'n.nid = p12.entity_id');
  $query->LeftJoin('taxonomy_term_data', 'gold_design_type', 'p12.field_gold_design_type_tid = gold_design_type.tid');

  $query->LeftJoin('field_data_field_gold_purity', 'p14', 'n.nid = p14.entity_id');
  $query->LeftJoin('taxonomy_term_data', 'gold_purity', 'p14.field_gold_purity_tid = gold_purity.tid');

  $query->LeftJoin('field_data_field_gold_ring_size', 'p16', 'n.nid = p16.entity_id');
  $query->LeftJoin('taxonomy_term_data', 'gold_ring_size', 'p16.field_gold_ring_size_tid = gold_ring_size.tid');

  $query->LeftJoin('field_data_field_gold_image', 'p18', 'n.nid = p18.entity_id');
  $query->LeftJoin('file_managed', 'gold_image', 'p18.field_gold_image_fid = gold_image.fid');

  $query->fields('gold_price', array('field_gold_price_value'))
     ->fields('gold_product_type', array('name'))
     ->fields('gold_price_range', array('name'))
     ->fields('gold_gender', array('name'))
     ->fields('gold_wearing_style', array('name'))
     ->fields('gold_color', array('name'))
     ->fields('gold_design_type', array('name'))
     ->fields('gold_purity', array('name'))
     ->fields('gold_ring_size', array('name'))
     ->fields('gold_image', array('filename'));

  // ->condition('n.type', $content_type)
  //->condition('gold_product_type.name', $product_type);
  $result = $query->execute();

  while($record = $result->fetchAll()) {
    print_r($record);
  }

}

And the response I am getting is like this:

Array
(
    [0] => stdClass Object
        (
            [field_gold_price_value] => 5678
            [name] => Ring
            [gold_price_range_name] => 5000-10000
            [gold_gender_name] => Male
            [gold_wearing_style_name] => Daily Wear
            [gold_color_name] => Yellow
            [gold_design_type_name] => Classic
            [gold_purity_name] => 22 KT (916)
            [gold_ring_size_name] => 24.5
            [filename] => andaaaaaajvq_2.jpg
        )

    [1] => stdClass Object
        (
            [field_gold_price_value] => 12345
            [name] => Pendant
            [gold_price_range_name] => 10000-15000
            [gold_gender_name] => Female
            [gold_wearing_style_name] => Casual Wear
            [gold_color_name] => Two Tone
            [gold_design_type_name] => 
            [gold_purity_name] => 
            [gold_ring_size_name] => 
            [filename] => n651311f_1.jpg
        )

    [2] => stdClass Object
        (
            [field_gold_price_value] => 150000
            [name] => Bangles
            [gold_price_range_name] => 100000-150000
            [gold_gender_name] => Female
            [gold_wearing_style_name] => Party Wear
            [gold_color_name] => Yellow
            [gold_design_type_name] => Band
            [gold_purity_name] => 22 KT (916)
            [gold_ring_size_name] => 12
            [filename] => different-gold-options-india.jpg
        )

    )

How to get the response in JSON format?

1 Answer 1

3

Replace this:

while($record = $result->fetchAll()) 
{
 print_r($record);
}

With this:

return $result->fetchAll();

And make sure you request the resource with the .json extension.

9
  • Thank you for your answer sir, I replaced as your suggestion and used the URL as brinvents.com/bin/jewels/api/details/retrieve.json, But now I am getting white screen. I don't know why?
    – param
    Aug 12, 2014 at 11:20
  • Check your server log for details on the WSOD. Aug 12, 2014 at 11:28
  • @param You haven't made the changes (your website is public and I can still the arrays being printed)
    – Clive
    Aug 12, 2014 at 11:29
  • Actually there are many functions are written in my custom module, There this is one of the function I am using.. This is the URL after changing,brinvents.com/bin/jewels/api/details/retrieve.json see sir Below two functions are not working means they are giving response as blank..
    – param
    Aug 12, 2014 at 11:36
  • I genuinely have no idea what you mean - that URL contains a bunch of printed PHP arrays, it's not blank. If you make the changes I specified in the answer the arrays won't be printed any more, and you'll see the json response.
    – Clive
    Aug 12, 2014 at 11:40

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.