1

I have the following JSON formatted string:

{

"hooks":[
        {
        "type":"subscribe",
        "id":1331741592.6925,
        "email":"JoeX@test-email.com",
        "status":"Active",
        "custom_fields":"{\"first_name\":\"Joe\",\"last_name\":\"X\"}",
        "ip_created":"24.199.200.142",
        "list_id":"33",
        "list_type":"internal",
        "list_name":"Administrator List 2",
        "list_optin":false
        },
        {
        "type":"subscribe",
        "id":1331741592.7067,
        "email":"JaneY@test-email.com",
        "status":"Active",
        "custom_fields":"{\"first_name\": \"Jane\",\"last_name\":\"Y\"}",
        "ip_created":"24.199.200.142",
        "list_id":"33",
        "list_type":"internal",
        "list_name":"Administrator List 2",
        "list_optin":false
        }
    ]

}

I want to use the PHP json_decode() function to put it in an associative array.

When I do run the script, debugging shows the value of the new array as null, so I presume the decode is failing. We aren't running PHP 5.3, so I can't use json_last_error(). Here is the code:

$hooks = (the JSON string from above);
$hooksArray = json_decode($hooks, true);

Any ideas why the $hooksArray is coming back null?

Sirko
  • 72,589
  • 19
  • 149
  • 183
csza
  • 29
  • 3
  • I have no clue why `$hooksArray` is coming back null because [it works for me](http://viper-7.com/eb8mCn)... – nickb Jun 27 '12 at 13:58
  • It works for me. `var_dump(json_decode(''));` dumps an object – Michael Berkowski Jun 27 '12 at 13:58
  • Just tested your scenario in PHP 5.3 and it just works. – Nick Stemerdink Jun 27 '12 at 14:00
  • what kind of debugging, if your using an ide debugger, you may well be trying to view the variable before the needed line of code has executed, try setting the debugger to pause on the line after. – nathan Jun 27 '12 at 14:00
  • @csza Are you sure that you don't misspell `$hooksArray` when you use it later? – Leri Jun 27 '12 at 14:02
  • this could be helpful: http://stackoverflow.com/questions/689185/json-decode-returns-null-php . Also a possible solution is to strip the newlines with the function on this post http://www.php.net/manual/de/function.json-decode.php#95782 – sofl Jun 27 '12 at 14:03
  • Are you using the breakpoint on the same line as where the array is assigned to $hooksArray? If so, it breaks before the value is assigned, so put the breakpoint on the line after and check the value of $hooksArray again. – Nick Stemerdink Jun 27 '12 at 14:07
  • The string is getting passed in via a $_POST arg, so it doesn't have the pretty-print that I used above. I did that to show the JSON in a more organized way in case someone saw something in there that I couldn't – csza Jun 27 '12 at 14:30
  • Do you have magic quotes enabled? http://php.net/manual/en/security.magicquotes.php. This might corrupt the JSON formatted string – Nick Stemerdink Jun 27 '12 at 14:32

1 Answers1

0

Is the JSON string inside your PHP source? Perhaps it's not interpreting the escaped backslashes correctly.

I tried the following experiment in Python for reference: Dumped the JSON data into a multi-line string via the REPL and decode it with json.loads(). It choked on the in-string quotes in the first instance of the custom_fields string. When I examined the multi-line string, all the escapes were gone, leaving me only with the quotes.

When I put the same JSON data in an external file and loaded it, it worked fine.

I put the JSON data in an external file and replaced all '\"' instances with '\\"' and then the first experiment started to work.

Maybe that will work for you too.

Multimedia Mike
  • 12,660
  • 5
  • 46
  • 62
  • Looks like it was pertaining to the "/". Thanks for the suggestion, wouldn't have thought that would choke it. Upgrading our PHP environment to 5.3 or later looks liek it would help some of this. Thanks for the input. – csza Jun 27 '12 at 14:43