1

I have

$string = $_REQUEST['COM_node'];

it contains the string

{"cmp_class":"ProfileReferences","auto_id":"cmp14","forms":[],"parent":{"cmp_class":"PrivateMediaNetworkList","auto_id":"httpdoc"}}

When I tried

$nodeArray      = json_decode($string, true); 

returns NULL. But when I given like

$string = '{"cmp_class":"ProfileReferences","auto_id":"cmp14","forms":[],"parent":{"cmp_class":"PrivateMediaNetworkList","auto_id":"httpdoc"}}';
$nodeArray      = json_decode($string, true); 

It works fine. I searched the web, but found no solution.

2
  • 1
    I wonder how you are not getting a syntax error for missing parenthesis. Commented Mar 6, 2014 at 14:14
  • better to visit the documentation : in1.php.net/json_decode Commented Mar 6, 2014 at 14:21

3 Answers 3

2

Are you sure that your $_REQUEST['COM_node'] contains the exact string without any hidden character like UTF-8 BOM or similar?

$string = chr(239).chr(187).chr(191).'{"cmp_class":"ProfileReferences","auto_id":"cmp14","forms":[],"parent":{"cmp_class":"PrivateMediaNetworkList","auto_id":"httpdoc"}}';
var_dump($string); // returns your string, although there are hidden chars

$nodeArray = json_decode($string,true);
var_dump($nodeArray); // returns NULL

Try comparing it with:

$string = '{"cmp_class":"ProfileReferences","auto_id":"cmp14","forms":[],"parent":{"cmp_class":"PrivateMediaNetworkList","auto_id":"httpdoc"}}';
var_dump($_REQUEST['COM_node'] == $string);

If the result is false, you'll need to find out, which characters to trim away.

EDIT: You can modify you string to get only the part beginning with the first { and ending with the last }

preg_match("/{(.*)}/",$string,$matches);
$string = $matches[0]; 
Sign up to request clarification or add additional context in comments.

1 Comment

Hmm. When I found strlen of $_REQUEST['COM_node'] and the string it self they are different
0

Passing true as the second parameter to json_decode() returns an array. You’ll have to use print_r() to view it; echo() won’t work.

php > $string = '{"cmp_class":"ProfileReferences","auto_id":"cmp14","forms":[],"parent":{"cmp_class":"PrivateMediaNetworkList","auto_id":"httpdoc"}}';
php > echo json_decode($string, true);
PHP Notice: Array to string conversion in php shell code on line 1

php > print_r($json_decode($string, true));
Array
(
    [cmp_class] => ProfileReferences
    [auto_id] => cmp14
    [forms] => Array
        (
        )

    [parent] => Array
        (
            [cmp_class] => PrivateMediaNetworkList
            [auto_id] => httpdoc
        )
)

Comments

0

The first steep to debug problem is to get an error. You may use the function json_last_error_msg() or pass the flag JSON_THROW_ON_ERROR to the json_decode().

https://www.php.net/manual/en/function.json-last-error-msg.php
https://www.php.net/manual/en/function.json-decode.php

The second steep would be to dump a hex representation of Your string. There are few invisible characters that may break JSON.

var_dump(bin2hex($string));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.