6

I have a Json string and I am decoding it using php's json_decode.

The string

            "address": {
                "address": null,
                "postalCode": null,
                "phoneNumber": "",
                "city": null
            }

When I decode the string I get

            ["address"]=>
                  array(1) {
                  ["phoneNumber"]=>
                       string(0) ""

It essentially strips the attributes with null as a value i.e address, city. Can I prevent this from happening.

COMPLETE JSON

            {"cost": null,
            "receiptNumber": null,
            "receiptType": null,
            "labNo": 596726,
            "parentLabNo": 0,
            "investigation": "BS for mps",
            "patient": {
                "id": 168967,
                "fullName": "UVOGIN",
                "dateOfBirth": "1972-04-04 00:00:00",
                "gender": "Male"
            },
            "address": {
                "address": null,
                "postalCode": null,
                "phoneNumber": "",
                "city": null
            }
        }
briankip
  • 2,502
  • 2
  • 23
  • 26
  • In theory anyway, `null` is the same as not being defined in the first place. This is why undefined variables, when accessed, are treated as null. – Niet the Dark Absol Oct 14 '14 at 15:03
  • provide the complete JSON – Dharam Oct 14 '14 at 15:03
  • 3
    [**Cannot confirm** - you must be doing something else with the data (like running it through `array_filter`)](http://3v4l.org/lvXTD). – h2ooooooo Oct 14 '14 at 15:04
  • added the complete json @Dharam – briankip Oct 15 '14 at 06:08
  • @h2ooooooo you are right, something else is gobbling up my nulls. – briankip Oct 15 '14 at 06:17
  • I'm having this same problem, and it's driving me crazy. My form uses `JSON.stringify(extraData)`, and some of the values in extraData are null. My Laravel controller receives the field input as a string, and the keys correctly appear even when there are null values. I've also tested this string in lint to ensure it's valid JSON. Literally the next line is `$extraData = json_decode($extraDataStr, true);`, and the keys that had null values are gone. I've written a unit test checking json_decode of that same string, and the keys remain correct. Only in my Laravel controller do they disappear. – Ryan Jan 16 '19 at 19:25
  • From https://stackoverflow.com/a/33298205/470749, I thought my problem might be fixed by updating from `Input::get()` to `$request->input()`, but it didn't help. Then, when I changed the JS on my form to provide empty strings instead of nulls, the keys did not get stripped out by `json_decode` in PHP. I'd love to solve why the keys with null values get removed from one line to the next in: `$extraDataStr = (string) $request->input('extraData'); $extraData = json_decode($extraDataStr, true);` Xdebug shows a valid JSON string for `$extraDataStr`, and then the nulls are gone in the next line. – Ryan Jan 16 '19 at 22:47

1 Answers1

2

The attributes are not stripped, you might be stripping it yourself doing something like explained here: strip null values of json object

See example of your code:

$test = '{"address": {
            "address": null,
            "postalCode": null,
            "phoneNumber": "",
            "city": null
        }}';

$test_decoded = json_decode($test,true);
print_r($test_decoded);

//outputs as expected:
//Array ( [address] => Array ( [address] => [postalCode] => [phoneNumber] => [city] => ) )
Community
  • 1
  • 1
fellowworldcitizen
  • 3,441
  • 3
  • 15
  • 17