I have the following JSON string coming back from a Node JS Service:
"{\"success\":true,\"isavailable\":true}"
When I try to use json_decode() in the PHP I get the following on a var_dump:
string '{"success":true,"isavailable":true}' (length=35)
So, I set about to do the following:
$str = str_replace("\"{", "'{", $str);
$str = str_replace("}\"", "}'", $str);
$str = str_replace('\"','"',$str);
When I do this I get a Syntax Error from Return Last Error and a Value of NULL.
So, what is the correct way to parse this JSON string in PHP?
The correct solution (assuming the input is exactly what is in the OP) is to use
trim()to get rid of the leading and trailing double quotes after callingstripslashes():Now, you get:
I’ve also confirmed that this string appears to be double-encoded, so it can also be properly decoded with: