Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json_decode double quotes and commas returns NULL

Tags:

json

php

I'm trying to use json_decode to decode a json that contains a word with a comma and a word with double quotes:

{"wordsFont":"Times New Roman","nameList":["Overflow ,","Stack ""]}

json_decode fails so i tried to format the string to escape the quotes with functions like:

function fixDoubleQuotedJSON($broken_json)
{
   return str_replace('""','\""',$broken_json);
}

but it's only the case where the comma and double quotes are at the end of a word. And the outcome:

{"wordsFont":"Times New Roman","nameList":["Overflow \,","Stack \""]}

decodes correctly but i need to find a way to do that for any placement of double quotes and commas. any suggestions?

EDIT: this is the initial value in the database:

{\"wordsFont\":\"Times New Roman\",\"nameList\":[\"Overflow ,\",\"Stack \\\"\"]}

and i can't change the code that generates it to the database

like image 497
Tom Avatar asked Oct 22 '25 16:10

Tom


1 Answers

My problem was fixed by replacing stripslashes() with urldecode(). This happened because someone previously made use of urlencode().

$jsonFix = urldecode($json);
$json = json_decode($jsonFix, true);
like image 70
Tom Avatar answered Oct 24 '25 06:10

Tom