In my code it is making Ajax request for json data. For JSON output I am using drupal_json() function.

But on browser side it is invoking error handler instead of success handler and getting 'parsererror'.

The JSON data is like this:

    "affiliate_urls": {
        "4131": {
            "nid": "4131",
            "title": "play.com - Books",
            "file_path": "sites/default/files/playlogo.gif",
            "url_value": "http://www.play.com/Search.html?searchstring=asd+asdasd\x26searchsource=0\x26searchtype=allproducts"

When put on http://jsonlint.com/ I am getting this error:

Parse error on line 7:
...       "url_value": "http://www.play.com
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['

What is wrong with passing URL value over JSON? and still using drupal_json().

Please help on it.


  • 2
    Problem is with this part: \x26. Now, if you are asking us about json, it's offtopic and should be asked on Stack Overflow. For example here. If you are asking us why Drupal generates bad json, show us the part of your Drupal code that generates it.
    – Mołot
    Sep 9, 2013 at 6:54
  • 1
    Issue root cause is the way json is being created and a perfect solution should stay on that level.The shared link by @Mołot gives a insight of technical reason for that.
    – arpitr
    Sep 9, 2013 at 7:18
  • @arpitr if you are saying it's a bug with Drupal 6 (drupal_json() function), it should be sent to it's issue queue - please flag it appropriately. If you think it's a problem with the way he is using it, please state it clearly. Now I'm not quite sure which case is it.
    – Mołot
    Sep 9, 2013 at 7:28
  • my believe is "the way he is using it". though I could have tested with drupal 7 function (drupal_encode_json())to encode a php array containing url to json and decode it back to php array, its all working good for me and I see drupal_to_js() and drupal_json_encode are equivalent functions.
    – arpitr
    Sep 9, 2013 at 8:10
  • 1
    @arpitr Drupal 7 uses json-related functions that are now part of PHP, only wraps them in Drupal API. When 6 was released, there was no native PHP functions for json, so it's using own implementation. So you tested functions with the same idea behind them, but wrote by different people.
    – Mołot
    Sep 9, 2013 at 8:24

1 Answer 1


The problem is with drupal_to_js() which is called by drupal_json() to do the job. drupal_to_js() will perform str_replace(array("\r", "\n", "<", ">", "&"), array('\r', '\n', '\x3c', '\x3e', '\x26'), addslashes($var)) to every string it receives. Converting '&' to '\x26' is making problem to me here. Anyway I cannot use json_encode() because of PHP dependency (Drupal 6).

I solved the problem by URL encoding the URL value on server side using PHP function urlencode() when preparing JSON data. And URL decoded it on browser side by JS function decodeURI().

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.