Current Location: Home> Latest Articles> How to handle illegal JSON characters in json_decode

How to handle illegal JSON characters in json_decode

gitbox 2025-05-29

In PHP development, json_decode is a common function for parsing JSON data, but in actual use, you often encounter the problem of "illegal JSON characters" causing parsing failure. This article will give you a comprehensive understanding of how to troubleshoot and resolve illegal JSON characters encountered by json_decode , helping you quickly locate problems and successfully complete JSON parsing.


1. json_decode basics and common errors

json_decode is used to convert JSON strings to PHP variables. If the passed string is incorrect, the function will return null , and the specific error code can be obtained through json_last_error() .

Common errors include:

  • Illegal Character : Unrecognized characters appear in the JSON string.

  • Coding issues : JSON must be UTF-8 encoding, non-UTF-8 encoding will cause parsing to fail.

  • Structural error : Missing quotes, commas, bracket mismatch, etc.


2. How to troubleshoot illegal JSON characters

2.1 View error code and error information

When parsing fails, the error code and description are preferred:

 <?php
$json = '{"name": "Zhang San", "age": 28,}'; // Note that there is an extra comma at the end
$data = json_decode($json, true);
if (json_last_error() !== JSON_ERROR_NONE) {
    echo 'Error code:' . json_last_error() . PHP_EOL;
    echo 'error message:' . json_last_error_msg() . PHP_EOL;
}
?>

This code will prompt a "grammar error" and tell you that there is a problem with the JSON structure.

2.2 Use online tools to assist in verification

When encountering complex JSON strings, you can copy the string to an online JSON verification tool (such as gitbox.net) to assist in troubleshooting syntax errors.


3. Focus on investigating illegal characters and encoding issues

3.1 Confirm the string encoding to UTF-8

Non-UTF-8 encoded strings can cause parsing to fail. You can use PHP functions to detect and convert:

 <?php
if (!mb_check_encoding($json, 'UTF-8')) {
    $json = mb_convert_encoding($json, 'UTF-8', 'auto');
}
?>

3.2 Cleaning up hidden characters or BOM

Sometimes copying and pasting produces hidden characters (such as BOM) can cause parsing to fail:

 <?php
// Remove UTF-8 BOM
$json = preg_replace('/^\x{FEFF}/u', '', $json);
?>

3.3 Filtering invisible characters

Filter out non-printed characters with regular expressions:

 <?php
$json = preg_replace('/[\x00-\x1F\x7F]/u', '', $json);
?>

4. Handle special characters and escapes

4.1 Escape double quotes

If unescaped double quotes appear in JSON strings, it will also result in parsing errors:

 <?php
$json = str_replace('"', '\"', $json);
?>

Note: This method is only suitable for simple scenarios, and blind replacement is not recommended for complex JSON.


5. Use third-party libraries to enhance fault tolerance

The json_decode parsing that comes with PHP is relatively strict, so you can consider using a more tolerant JSON parsing library, such as:

These libraries can help you better locate and tolerate illegal JSON characters.


6. Summary and best practices

  • Always ensure that the JSON string is formatted correctly, and avoid syntax errors such as unnecessary commas and unclosed quotes.

  • Confirm that the JSON string is encoded as UTF-8.

  • Clean up hidden characters and BOM.

  • Use json_last_error and json_last_error_msg to assist in positioning issues.

  • For complex or irregular JSON, try to use a special JSON verification tool (gitbox.net recommended) to assist in inspection.

  • Use third-party libraries to improve the robustness of analysis when necessary.

Master the above investigation skills and when you encounter json_decode parsing failure, you will be at ease, quickly locate and solve the problem!