0

Here is my code:

    var_dump(json_decode($data['event']->options['meals']['options'][0]['option'], true));
    echo '<br />';echo '<br />';
    var_dump($data['event']->options['meals']['options'][0]['option']);
    echo '<br />';echo '<br />';
    var_dump(json_decode('[{"name":"Petit Tenderloin","description":"Wrapped in Apple Wood Bacon, borsoun whipped mashed potatoes, roasted baby vegetable, with sun dried cherry sauce. "},{"name":"Chicken Piccatta","description":"In lemon caper sauce, served with a timbal of wild rice and vegetables. "}]', true));

Here is my output:

NULL

string(279) "[{"name":"Petit Tenderloin","description":"Wrapped in Apple Wood Bacon, borsoun whipped mashed potatoes, roasted baby vegetable, with sun dried cherry sauce. "},{"name":"Chicken Piccatta","description":"In lemon caper sauce, served with a timbal of wild rice and vegetables. "}]"

array(2) { [0]=> array(2) { ["name"]=> string(16) "Petit Tenderloin" ["description"]=> string(115) "Wrapped in Apple Wood Bacon, borsoun whipped mashed potatoes, roasted baby vegetable, with sun dried cherry sauce. " } [1]=> array(2) { ["name"]=> string(16) "Chicken Piccatta" ["description"]=> string(72) "In lemon caper sauce, served with a timbal of wild rice and vegetables. " } }

Why is it that when I put in a string literal I get the proper array, but when I pass in a variable I get NULL? I feel like I am missing something super simple....

EDIT: Found out reason Looks like the variable has a new line character that naturally doesn't show up in the HTML. Looks like the new line char breaks json_decode...

Anyone know of a way around that other than removing the new lines? (I'd prefer to keep them in if I can)

Aram Papazian
  • 2,453
  • 6
  • 38
  • 45
  • Not sure, but after your first var_dump, you could try using [json_last_error](http://php.net/manual/en/function.json-last-error.php) to try and determine why you're getting NULL. My guess would be that you have some encoding going on, that gets clobbered when you do the var_dump. – ernie Dec 05 '12 at 00:58

2 Answers2

1

Make sure that the array has data on the first line where you var_dump its contents. I was unable to reproduce your error.

My code:

<?php

$data['event']->options['meals']['options'][0]['option'] = '[{"name":"Petit Tenderloin","description":"Wrapped in Apple Wood Bacon, borsoun whipped mashed potatoes, roasted baby vegetable, with sun dried cherry sauce. "},{"name":"Chicken Piccatta","description":"In lemon caper sauce, served with a timbal of wild rice and vegetables. "}]';

var_dump(json_decode($data['event']->options['meals']['options'][0]['option'], true));
echo '<br />';echo '<br />';
var_dump($data['event']->options['meals']['options'][0]['option']);
echo '<br />';echo '<br />';
var_dump(json_decode('[{"name":"Petit Tenderloin","description":"Wrapped in Apple Wood Bacon, borsoun whipped mashed potatoes, roasted baby vegetable, with sun dried cherry sauce. "},{"name":"Chicken Piccatta","description":"In lemon caper sauce, served with a timbal of wild rice and vegetables. "}]', true));

?>

This was the output it produced for me:

array(2) {
  [0] =>
  array(2) {
    'name' =>
    string(16) "Petit Tenderloin"
    'description' =>
    string(115) "Wrapped in Apple Wood Bacon, borsoun whipped mashed potatoes, roasted baby vegetable, with sun dried cherry sauce. "
  }
  [1] =>
  array(2) {
    'name' =>
    string(16) "Chicken Piccatta"
    'description' =>
    string(72) "In lemon caper sauce, served with a timbal of wild rice and vegetables. "
  }
}
<br /><br />
string(278) "[{"name":"Petit Tenderloin","description":"Wrapped in Apple Wood Bacon, borsoun whipped mashed potatoes, roasted baby vegetable, with sun dried cherry sauce. "},{"name":"Chicken Piccatta","description":"In lemon caper sauce, served with a timbal of wild rice and vegetables. "}]"
<br /><br />
array(2) {
  [0] =>
  array(2) {
    'name' =>
    string(16) "Petit Tenderloin"
    'description' =>
    string(115) "Wrapped in Apple Wood Bacon, borsoun whipped mashed potatoes, roasted baby vegetable, with sun dried cherry sauce. "
  }
  [1] =>
  array(2) {
    'name' =>
    string(16) "Chicken Piccatta"
    'description' =>
    string(72) "In lemon caper sauce, served with a timbal of wild rice and vegetables. "
  }
}
Daniel Miladinov
  • 1,582
  • 9
  • 20
  • You can tell it has data because on the 3rd line without altering the code it outputs the correct string... That's why I'm confused. When passing in the variable it's failing, but when I pass in the string literal, it succeeds... – Aram Papazian Dec 05 '12 at 00:53
  • And if I assign the string to an $x and do a json_decode on $x I get the proper details... so it looks like it's potentially something to do with that variable... – Aram Papazian Dec 05 '12 at 00:55
0

The unescaped new line character will break json_decode as that's not valid JSON.

Previous question on escaping line breaks in JSON.

Check out eyelidlessness' answer there for keeping the line-breaks. Short version is that you need to escape them, e.g.:

$text = str_replace("\n", "\\n", $text);

Alternatively, you may want to replace newlines with <br>, insetad of escaping them for rendering in a browser.

You've got GIGO. Not sure if you're controlling the input, but if you are, then you should escape it on the front end, using json_encode, which will automatically escape (and thus preserve) the newlines.

Community
  • 1
  • 1
ernie
  • 6,356
  • 23
  • 28