[Solved] How can i decode data from JSON.stringify


Your problem is the invalid JSON text.

I’ve made an attempt to fix the json. Be aware, it’s ugly. Very ugly!

So, having this as input:

$a="{"LinkedIn /*trimmed data to fit this format*/ Recruiting"}]}";
// you use $a = $_POST['LiRecData'];

What I did was try to reconstruct the JSON based on some (few) existing/correct elements:

$a = str_replace(["\n","\t",'<br>','</br>'],'',$a);       // remove new lines, tabs and br tags
$a = preg_replace ( '/([^"])\,([^"])/' , "$1___$2" , $a); // replace commas inside qoutes with SOMETHING
$a = preg_replace ( '/([^"])\:([^"])/' , "$1__|$2" , $a); // replace semicolon inside qoutes with SOMETHING ELSE
$a = str_replace('"','',$a);                              // remove all qoutes
$a = str_replace(':','":"',$a);                           // quote all semicolons 
$a = str_replace(',','","',$a);                           // quote all commas
$a = preg_replace ( '/([\{])([a-zA-Z0-9])/' , "$1\"$2" , $a); // {A => {"A
$a = preg_replace ( '/([\:])\"\s([\[])/' , "$1$2" , $a);      // : " [ => : [
$a = preg_replace ( '/([a-zA-Z0-9])([\}])/' , "$1\"$2" , $a); // adas} => adas"}
$a = preg_replace ( '/([\.])(\s{0,})([\}])/' , "$1\"$3" , $a);
$a = preg_replace ( '/([\"])(\s{1,})([a-zA-Z0-9])/' , "$1$3" , $a); // "   Key" => "Key" trim keys
$a = str_replace(':" }',':"" }',$a);
$a = str_replace(']","','],"', $a);
$a = str_replace("___",",",$a);             // revert comment 2
$a = str_replace("__|",":",$a);             // revert comment 3
$a = str_replace(']}","Top', '],"Top',$a);  //manual fix for TopSkils

print_r(json_decode($a,true));

You can see it here:

https://3v4l.org/QdaiV

4

solved How can i decode data from JSON.stringify