Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP json_decode() returns NULL with valid JSON?

Tags:

json

php

null

People also ask

What does json_decode 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.

How do you check a JSON is valid or not?

The simplest way to check if JSON is valid is to load the JSON into a JObject or JArray and then use the IsValid(JToken, JsonSchema) method with the JSON Schema. To get validation error messages, use the IsValid(JToken, JsonSchema, IList<String> ) or Validate(JToken, JsonSchema, ValidationEventHandler) overloads.

Is valid JSON in PHP?

Check for valid JSON we can use json_last_error() PHP function to check validity of JSON, It returns JSON_ERROR_NONE predefined constant value if JSON successfully decoded or encoded by PHP's josn functions.

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.


This worked for me

json_decode( preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $json_string), true );

It could be the encoding of the special characters. You could ask json_last_error() to get definite information.

Update: The issue is solved, look at the "Solution" paragraph in the question.


You could try with it.

json_decode(stripslashes($_POST['data']))

If you check the the request in chrome you will see that the JSON is text, so there has been blank code added to the JSON.

You can clear it by using

$k=preg_replace('/\s+/', '',$k);

Then you can use:

json_decode($k)

print_r will then show the array.


Maybe some hidden characters are messing with your json, try this:

$json = utf8_encode($yourString);
$data = json_decode($json);