Search code examples
phpjsonjson-deserialization

How to reverse json_decode($myJsonString, assoc=true) to get again $myJsonString?


I'm trying to reverse an operation with json_decode to get again the string.

The problem is that I can't do json_encode with the result because the first transformation happened with the parameter assoc=true

$myJsonString = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

var_dump(json_decode($json, true));

Result:

array(5) {
    ["a"] => int(1)
    ["b"] => int(2)
    ["c"] => int(3)
    ["d"] => int(4)
    ["e"] => int(5)
}

I would like to recover the String in order to perform json_decode without second parameter assoc=true


Solution

  • Use the function json_encode() to create a json string again (as it returns string):

    $myJsonString = json_encode($my_decoded_json);