I was develop application which support json, and my valid json return null when i was trying to decode with php function (json_decode())
this is problem make me headache.
VALID JSON
You have to sure that your JSON data are valid, to make you easier you can find json validator online at google. Copy Paste your code to online validator and let’s see if your JSON are valid or not.
In my case I use http://jsonlint.com/ and http://jsonformatter.curiousconcept.com/
Opppsss….!!!
My JSON are valid but still return null with PHP.
FIND ERROR
If you check with validator there no error then you have to know what PHP said :
use following code to check it up in your code
json_decode( $json_data, true, 9 ) $json_errors = array( JSON_ERROR_NONE => 'No error has occurred', JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded', JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded', JSON_ERROR_SYNTAX => 'Syntax error', ); echo 'Founded Error : ', $json_errors[json_last_error()], PHP_EOL, PHP_EOL;
after you try this you may know what php say about your json code and you can fix it.
In my case as I mention above , php said error. that’s funny becuase online validator said data are valid.
CLEAN JSON DATA
cause of my case I create function to clean special character like space \n \t etc
after i use it wow… finally my json data are valid and not return NULL anymore.
here is the function in php
function json_clean($string){
function clean_json($string) {
// for end of line (or begining)
$string = trim($string);
// from everywhere
$string = str_replace("\n", "", $string);
$string = str_replace("\r", "", $string);
return $string;
}
}
—————————————————————————
in another occasion you may face with diffrent problem with me
fell free to ask it with form bellow, we can find solution together.