Is it possible to put the Json with unescaped quotes?

Has an API which returns a json response in this response are escaped by double quotes for example:
{"name": "my "name,"", "value": "123456789"}

In the end, json_decode can read it, because it turns out the json is invalid, can I something to do that would be to read it? API is not mine and to fix it I can't.
March 23rd 20 at 19:44
3 answers
March 23rd 20 at 19:46
Solution
You can try to fix broken JSON.
Something like this, but it does not cover 100% of your question
<?php
$str = '{"name": "my "name,"", "value": "123456789"}';

fixJSON function($str) {
 $len = strlen($str);
 $result = ";
 $space = ";
 $mode = 0;
for($i=0;$i<$len;$i++){
 $c = $str[$i];
 switch ($mode) {
 case 0: //outside line
 if ($c == '"') $mode = 1;
 $result .= $c;
break;
 case 1: //inside string
 if ($c == '"') {
 $mode = 3;
 $space = $c;
break;
}
 if ($c == '\\') $mode = 2;
 $result .= $c;
break;
 case 2: //immediately after the slash (ignore any single character)
 $mode = 1;
 $result .= $c;
break;
 case 3: //there was a quote inside the string
 if ($c == '' || $c == '\n' || $c == '\t' || $c == '\r')
 $space .= $c;
 elseif ($c == ',' || $c == '}' || $c == ']' || $c == ':') {
 //probably was the end of the line (chance of 99%)
 $mode = 0;
 $result .= $space.$c;
 } else {
 //we stayed inside the lines 100%. Escapes
 $mode = 1;
 $result .= '\\'.$space;
 $i--; //go back, because suddenly it again quote
}
break;
}
}
 return $result;
}

echo(fixJSON($str)); //{"name": "my \"name\"", "value": "123456789"}
?>

But it is better to fix the place which leads to breakage JSON to there to you from the normal line came, not crutches to invent.

PS
More perfect and more stoned variant
<?php
$str = '{"say": "of"my name", hi", "value": "123456789"}';

fixJSON function($str) {
 $len = strlen($str);
 $result = ";
 $space = ";
 $mode = 0;
for($i=0;$i<$len;$i++){
 $c = $str[$i];
 switch ($mode) {
 case 0: //outside line
 if ($c == '"') $mode = 1;
 $result .= $c;
break;
 case 1: //inside string
 if ($c == '"') {
 $mode = 3;
 $space = $c;
break;
}
 if ($c == '\\') $mode = 2;
 $result .= $c;
break;
 case 2: //immediately after the slash (ignore any single character)
 $mode = 1;
 $result .= $c;
break;
 case 3: //there was a quote inside the string
 if ($c == '' || $c == '\n' || $c == '\t' || $c == '\r')
 $space .= $c;
 elseif ($c == '}' || $c == ']') {
 //probably was the end of the line (chance of 99%)
 $mode = 4;
 $space .= $c;
}
 elseif ($c == ',' || $c == ':') {
 //probably was the end of the line (chance 90%)
 $mode = 5;
 $space .= $c;
 } else {
 //we stayed inside the lines 100%. Escapes
 $mode = 1;
 $result .= '\\'.$space;
 $i--; //go back, because suddenly it again quote
}
break;
 case 4: //after special character
 if ($c == '' || $c == '\n' || $c == '\t' || $c == '\r')
 $space .= $c;
 elseif ($c == ',') {
 //was the end of the line with a chance of 99.99%
 $mode = 0;
 $result .= $space.$c;
 } else {
 //inside the string 100%
 $mode = 1;
 $result .= '\\'.$space;
$i--;
}
break;
 case 5: //after the decimal point
 if ($c == '' || $c == '\n' || $c == '\t' || $c == '\r')
 $space .= $c;
 elseif (preg_match('/[0-9"-]+/', $c)) {
 //was the end of the line with a chance of 99.9%
 $mode = 0;
 $result .= $space;
$i--;
 } else {
 //inside the string 100%
 $mode = 1;
 $result .= '\\'.$space;
$i--;
}
break;
}
}
 if ($mode>2) $result .= $space;
 return $result;
}

echo(fixJSON($str)); //{"say": "\"my name\", hi", "value": "123456789"}
?>
But it is always possible to put another string to JSON and no ultimate fixer will not cope with such depth.
March 23rd 20 at 19:48
Regular expression to replace quotation marks, but a start will have characters from the json syntax, so if they are in the text, it will error. This method is suitable only if you are sure that there is just text, or are you satisfied with the percentage of errors that you get.
March 23rd 20 at 19:50
No

Find more questions by tags JSONPHP