Resolve PHP json decode error: Difference between revisions

From LemonWiki共筆
Jump to navigation Jump to search
 
(One intermediate revision by one other user not shown)
Line 32: Line 32:
* [https://php.net/manual/en/function.json-last-error.php PHP: json_last_error - Manual]
* [https://php.net/manual/en/function.json-last-error.php PHP: json_last_error - Manual]
* [https://github.com/Seldaek/jsonlint Seldaek/jsonlint: JSON Lint for PHP]
* [https://github.com/Seldaek/jsonlint Seldaek/jsonlint: JSON Lint for PHP]
* [https://www.php.net/manual/en/class.jsonexception.php PHP: JsonException - Manual] (PHP 7.3.0)


[[Category:Data Science]] [[Category:PHP]] [[Category:Json]] [[Category:Text file processing]]
[[Category:Data Science]] [[Category:PHP]] [[Category:Json]] [[Category:String manipulation]]

Latest revision as of 19:32, 9 March 2021

Syntax error, malformed JSON[edit]

Possible causes

  • the input is empty entirely or combination of multiple white spaces
  • the input contains Byte order mark (BOM)[1]
  • One file was combined with multiple JSON which were separated by return symbol
  • Use ' instead of " for quotation

Solution:

Unexpected control character found[edit]

Possible causes

Solution:

  • Remove control character. Using PHP:
$replacement = '';
preg_replace('/[\x00-\x1F]/', $replacement, $input);

References[edit]

Relates articles[edit]