I'm getting a data feed which is in JSON format and the only available format. In PHP, I'm using json_decode to decode the JSON, but it was breaking, and I found out that the JSON was generated in some places with double quotes in a person's nick name. I verified this using: http://jsonformatter.curiousconcept.com
I don't have control over the creation of the data, but I have to deal with this broken format when it occurs. This data after it's parsed will be put into a MySQL TABLE.
For example:
"contact1": "David "Dave" Letterman",
json_decode would return a NULL. If I manually saved the file, and changed it to single quotes around the nickname of Dave, then everything worked.
$json_string = file_get_contents($json_download);
$json_array = json_decode($json_string, true);
How do I fix the broken JSON format in json_string before it gets processed by json_decode? What should be done to pre-process the file, backslash the double quotes of the nickname? Or change them to single quotes? Is it even a good idea to store double quotes like this in MySQL?
I don't know when this might occur with each data feed, so I don't want to just check for contact1 if it has inner double quotes to fix them. Is there a way in PHP to take a line such as the above example, and backslash everything after the colon except the outer double quotes? Thanks!
This is the correct code for as provided by tftd:
<?php
// This:
// "contact1": "David "Dave" Letterman",
// Needs to look like this to be decoded by JSON:
// "contact1": "David \"Dave\" Letterman",
$data ='"contact1": "David "Dave" Letterman",';
function replace($match){
$key = trim($match[1]);
$val = trim($match[2]);
if($val[0] == '"')
$val = '"'.addslashes(substr($val, 1, -1)).'"';
else if($val[0] == "'")
$val = "'".addslashes(substr($val, 1, -1))."'";
return $key.": ".$val;
}
$preg = preg_replace_callback("#([^{:]*):([^,}]*)#i",'replace',$data);
var_dump($preg);
$json_array = json_decode($preg);
var_dump($json_array);
echo $json_array . "\n";
echo $preg . "\n";
?>
Here is the output:
string(39) ""contact1": "David \"Dave\" Letterman","
NULL
"contact1": "David \"Dave\" Letterman",