Home » Php » json – New lines and tabs in json_decode() (PHP 7)

json – New lines and tabs in json_decode() (PHP 7)

Posted by: admin July 12, 2020 Leave a comment

Questions:

My code using json_decode() worked correctly with PHP 5.6. After migration to PHP 7.0, json_decode() returns NULL and json_last_error() tells me that my error is:

Control character error, possibly incorrectly encoded

After debugging, I found out that my problem are both tabs and new line characters in string values. If I remove them both, it works. If I leave either new lines or tabs, the error occurs.

Is json_decode() behavior changed in PHP 7? I would like to keep tabs and new lines in my .json files for better readability. The code works if I replace tabs to \t and new lines to \n.

How can I keep new lines and tabs?

How to&Answers:

Due to a software licensing issue, the old json module was replaced with the jsond module. You can see the discussion of this change and the attached pull request here. Now, there’s not much information about the changes or about workarounds for things, but I can see that all control characters inside strings ([\0x00-\0x1F]) trigger an error. Unfortunately for you, it seems that this behavior is correct per the JSON Standard:

Insignificant whitespace is allowed before or after any token. The whitespace characters are: character tabulation (U+0009), line feed (U+000A), carriage return (U+000D), and space (U+0020). Whitespace is not allowed within any token, except that space is allowed in strings.

So, in other words, literal tabs are not allowed inside JSON strings at all; they must be \t or \u0009. So, the JSON you’re consuming is in direct violation of the standard. Ideally, you should get your JSON source to return standards-compliant JSON. If that won’t work, you’ll have to pre-process the JSON and convert tabs inside strings to \t.

Answer:

Tabs and newline characters, unencoded, are not allowed in strings in JSON according to RFC 7159. Therefore, whatever program you used to encode has encoded a non-standard form of JSON. Unfortunately, json_decode‘s only option, JSON_BIGINT_AS_STRING, doesn’t help in this case.

Answer:

I had same problem with new lines and i cant modify the string that i received. I needed to work with it and my trick was to detect the new lines and rewrite it again, ‘literaly’!:

Before:

echo json_decode($json); // error JSON_ERROR_CTRL_CHAR

After:

$json = str_replace("\r\n", '\r\n', $json); // single quotes do the trick
echo json_decode($json); // works!