[Solved] PHP json_encode and javascript functions

I need to encode a javascript function into a JSON object in PHP.


$function = "function(){}";
$message = "Hello";

$json = array(   
      'message' => $message,
      'func' => $function
echo json_encode($json);



What I want is:


Can I do this with json_encode?

Solution #1:

As Jani said, this is not possible directly with JSON, but this might help you:

Respondent: Fabien Ménager

Solution #2:

No. JSON spec does not support functions. You can write your own code to output it in a JSON-like format and it should work fine though.

Respondent: Jani Hartikainen

Solution #3:

If don’t want to write your own JSON encoder you can resort to Zend_Json, the JSON encoder for the Zend Framework. It includes the capability to cope with JSON expressions.

Respondent: Stefan Gehrig

Solution #4:

json_decode parse the given array to json string, so you can play with it as a string. Just use some unique string to indicate the start and the end of the function. Then use str_replace to remove the quotes.

$function = "#!!function(){}!!#"; 
$message = "Hello";

$json = array(   
  'message' => $message,
  'func' => $function
$string = json_encode($json);
$string = str_replace('"#!!','',$string);
$string = str_replace('!!#"','',$string);
echo $string;

The output will be:

Respondent: mirmidon

Solution #5:

I wrote a small library that allows to do this. It’s similar to Zend framworks’s solution, but this library is much more lightweight as it uses the built-in json_encode function. It is also easier to use with external libraries, where json_encode is buried deeply in vendor code.

    use BalpingJsonRawRaw;
    use BalpingJsonRawEncoder;

    $array = [
        'type' => 'cat',
        'count' => 42,
        'callback' => new Raw('function(a){alert(a);}')

    let bar = <?php echo Encoder::encode($array); ?>
    bar.callback('hello'); //prints hello
Respondent: balping

Solution #6:

I write this simple function for all json function based help my myabe help someone:

function json_encode_ex($array) {
    $var = json_encode($array);
    preg_match_all('/"function.*?"/', $var, $matches);
    foreach ($matches[0] as $key => $value) {
        $newval = str_replace(array('n', 't','/'), array(PHP_EOL,"t",'/'), trim($value, '"'));
        $var = str_replace($value, $newval, $var);
    return $var;
Respondent: A1Gard

Solution #7:

You can try this:

var json_string = '{"message":"Hello","myfunc":"function(){ alert(this.message) }"}';
var json_string = JSON.parse(json_string, function(k,v){
    if(typeof v == 'string' && /function(){/.test(v)){
        return eval(k+' = '+v); 
        return v
Respondent: Adam

Solution #8:

The enconding part in PHP is seems to be solved by now. You can use


this way your function will not be escaped. However

Respondent: Delcon

Solution #9:

This function can also help:

function jsonify($var){
return str_ireplace(array("'function",'"function',"}'",'}"'),array("function",'function',"}",'}'),json_encode($var));

Taken from here:

Respondent: Vicky Jain

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy