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?