Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

json

php

php-7

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?

like image 511
Robo Robok Avatar asked Dec 28 '15 00:12

Robo Robok


People also ask

What json_decode () function will return?

The json_decode() function can return a value encoded in JSON in appropriate PHP type. The values true, false, and null is returned as TRUE, FALSE, and NULL respectively. The NULL is returned if JSON can't be decoded or if the encoded data is deeper than the recursion limit.

What is the use of json_decode in PHP?

The json_decode() function is used to decode or convert a JSON object to a PHP object.

What is json_encode function in PHP?

The json_encode() function is an inbuilt function in PHP which is used to convert PHP array or object into JSON representation. Syntax : string json_encode( $value, $option, $depth ) Parameters: $value: It is a mandatory parameter which defines the value to be encoded.

How can I get JSON encoded data in PHP?

To receive JSON string we can use the “php://input” along with the function file_get_contents() which helps us receive JSON data as a file and read it into a string. Later, we can use the json_decode() function to decode the JSON string.


3 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.

like image 157
Will Avatar answered Oct 18 '22 00:10

Will


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!
like image 30
David Avatar answered Oct 18 '22 02:10

David


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.

like image 26
Peter O. Avatar answered Oct 18 '22 02:10

Peter O.