6.x common.inc drupal_json($var = NULL)

Return data in JSON format.

This function should be used for JavaScript callback functions returning data in JSON format. It sets the header for JavaScript output.


$var: (optional) If set, the variable will be converted to JSON and output.

8 calls to drupal_json()
book_form_update in modules/book/book.pages.inc
Renders a new parent page select element when the book selection changes.
poll_choice_js in modules/poll/poll.module
Menu callback for AHAH additions.
profile_admin_settings_autocomplete in modules/profile/profile.admin.inc
Retrieve a pipe delimited string of autocomplete suggestions for profile categories
profile_autocomplete in modules/profile/profile.pages.inc
Callback to allow autocomplete of profile text fields.
system_date_time_lookup in modules/system/system.admin.inc
Return the date for a given format string via Ajax.

... See full list

1 string reference to 'drupal_json'
system_menu in modules/system/system.module
Implementation of hook_menu().


includes/common.inc, line 2614
Common functions that many Drupal modules will need to reference.


function drupal_json($var = NULL) {

  // We are returning JSON, so tell the browser.
  drupal_set_header('Content-Type: application/json');
  if (isset($var)) {
    echo drupal_to_js($var);


jhodgdon’s picture

tpainton’s picture

Overlooked by me. The print

<?php print drupal_json($data) ?>

is not necessary.

corbacho’s picture

Using jquery's ajax functions you can have problems with IE7 and "expected semicolon" errors, because of the header is not correctly interpreted.

It's too late to fix it in D6, so make your own function, using

drupal_add_http_header('Content-Type', 'application/json');,

That's the correct header, and Drupal 7 is using it also.

mshmsh5000’s picture

drupal_add_http_header() is a D7 function. To roll your own JSON response function in D6, use drupal_set_header(). E.g.:

drupal_set_header('Content-Type: application/json');
dark_kz’s picture

Why there is no charset in drupal_json function?

drupal_set_header("Content-Type: application/json; charset=utf-8");
Rob230’s picture

Please note that drupal_json() outputs non-correct JSON, which jQuery and some browsers will fail to parse.

For me this was caused by the fact that drupal_to_js() uses addslashes(), which escapes single quotes - this is not valid JSON, so jQuery fails due to an invalid escape character.

It seems unlikely that it's going to be fixed now. If you must still use Drupal 6 then your best bet is to use PHP's json_encode():

drupal_set_header('Content-Type: application/json');

Relevant issues:
https://drupal.org/node/512130 (originally a Drupal 5 issue; still unfixed after 5 years)
https://drupal.org/node/1086098 (major bug, still unfixed)

debaryadas’s picture

I have find out a solution for drupal_json when the output string contain single quotes. Suppose $var is the variable which need to be output as JSON. Then




$content = ""; // Define a variable to hold the JSON output from drupal_json
ob_start();	  // Turn the output buffering on so that output not sent to the browser
drupal_json($var); // Generate the JSON output
$content = ob_get_contents(); // Store it in a variable
ob_end_clean(); // Clean (erase) the output buffer and turn off output buffering
print stripslashes($content); // Output the string after escaping the single quotes