1

I have a Laravel app where this is in a Controller function:

$extraDataStr = (string) $request->input('extraData');
$extraData = json_decode($extraDataStr, true);

But even though $extraDataStr is valid JSON (as shown in Xdebug), $extraData becomes an array that contains only keys that had non-null values in $extraDataStr. This doesn't make any sense.

I thought I was losing my mind, so I even wrote a test of json_decode:

$extraDataStr = json_encode([
    'willBeNull' => null,
    'hasVal' => 4
]);
$extraDataArr = json_decode($extraDataStr, true);
$this->assertEquals(['willBeNull', 'hasVal'], array_keys($extraDataArr));
$this->assertNull($extraDataArr['willBeNull']);

It passes, of course.

So then I took that same line from the test and temporarily inserted it into the Controller function just to see whether somehow it would remove the nulls:

$extraDataStr = json_encode([
    'willBeNull' => null,
    'hasVal' => 4
]);
$extraData = json_decode($extraDataStr, true);

This erroneously omits keys with null values even when I'm using this basic json_encoded string rather than user input!

What the hell is going on?

P.S. This is not the same question as php json_decode removing attributes with null value because I've shown a test that passes and the exact code that is failing.

Ryan
  • 22,332
  • 31
  • 176
  • 357
  • 2
    The last snippet that you provided doesn't seem to omit any key for me. How do you go through the result to verify that? – Aioros Jan 16 '19 at 23:06
  • Check [this](https://gist.github.com/tureki/7af3ea2554eba60c34c5). – Tpojka Jan 16 '19 at 23:14
  • How are you checking `$extraData`? Just dumping it, or what? – Don't Panic Jan 16 '19 at 23:22
  • @Aioros First, I noticed in the database that the data looked wrong. Then I ran Xdebug to step through the code line by line. Immediately after the `json_decode` line, `$extraData` shows an array that is incomplete: it contains only the key-value pairs where values were non-null. – Ryan Jan 16 '19 at 23:25
  • @Don'tPanic See my previous comment. Thanks. – Ryan Jan 16 '19 at 23:25
  • @Tpojka What I *don't* want is for the nulls to get removed. Those examples feel like what is happening, but I'm only using `json_decode` and none of that. – Ryan Jan 16 '19 at 23:27
  • 1
    Hmm, interesting. Are you viewing the xdebug output in an IDE? – Don't Panic Jan 16 '19 at 23:33
  • 2
    It could be a problem with Xdebug though. Right after that json_decode, couldn't you check directly if the array has the right keys? Either with a full `var_dump` or with something like `array_key_exists('willBeNull', $extraData)` – Aioros Jan 16 '19 at 23:52
  • @Don'tPanic Yes, Netbeans. – Ryan Jan 17 '19 at 00:14
  • 1
    @Aioros and @Don'tPanic, you seem to be right. I think it was just Netbeans and Xdebug irresponsibly excluding nulls from the display. I ran `$extraData = json_decode($extraDataStr, true); Log::debug('reencoded: ' . json_encode($extraData)); var_dump($extraData); dd($extraData);` and see them there. Thanks! – Ryan Jan 17 '19 at 00:15
  • 2
    I found this old post when I was looking for possible causes of this earlier: https://stackoverflow.com/questions/3473437/netbeans-xdebug-works-but-wont-expose-some-php-variables Maybe it's that filter thing? I haven't used Netbeans in a few years, so I don't really remember much about that. – Don't Panic Jan 17 '19 at 00:23
  • 1
    @Don'tPanic Boom! That was it! Thanks so much! – Ryan Jan 17 '19 at 00:31
  • 1
    Great! Glad I could help. Man, I really hate it when stuff like that happens - something that seems like it should "just work" mysteriously doesn't. – Don't Panic Jan 17 '19 at 00:33

0 Answers0