1

Trying to save a php array of objects into a json file, but boolean properties are being saved in strings :

[
    {
        "title" : "My Page",
        "url"   : "mypage",
        "type"  : "content",
        "final" : "false" // supposed to be simply false
    }
]

why is it? ... is there a flag I could use or something? currently I am using JSON_PRETTY_PRINT|JSON_NUMERIC_CHECK

1 Answers1

2

I have a feeling that the conversion is working correctly, and that the value actually is a string. You can confirm with gettype($var).

Please note that URL-encoding only gives you string values. You could try switching to JSON.

To give PHP the ability to handle application/json, add this function, and then call it:

function convertJsonBody() {
     $methodsWithDataInBody = array(
          'POST',
          'PUT',
     );

     if (
          isset($_SERVER['CONTENT_TYPE'])
          && (strpos(strtolower($_SERVER['CONTENT_TYPE']), 'application/json') !== FALSE)
          && isset($_SERVER['REQUEST_METHOD'])
          && in_array($_SERVER['REQUEST_METHOD'], $methodsWithDataInBody)
     ) {
          $_POST = json_decode(file_get_contents('php://input'), TRUE);
          foreach($_POST as $key => $value) {
               $_REQUEST[$key] = $value;
          }
     }
} 
Harry Pehkonen
  • 3,038
  • 2
  • 17
  • 19
  • you are right, it might be something in the way I am passing the data to the server. i'll try to change that –  Feb 19 '17 at 21:52
  • the data in header is being passed like so : data[1][final]:true - is there any further processing which is needed to be done on server upon receiving the data? –  Feb 19 '17 at 21:59
  • the content type is currently : `application/x-www-form-urlencoded` –  Feb 19 '17 at 22:00
  • 1
    If you are using urlencoding, all the values will be strings. You will have to convert to boolean. Or you can switch to using JSON content type ("application/json"). – Harry Pehkonen Feb 19 '17 at 22:02
  • so now I have switched to `application/json` content-type, but $_POST is showing empty,the request payload seems to be ok on the network tab... –  Feb 19 '17 at 22:11
  • this post here says that `application/json` isn't natively supported by php and should use `application/x-www-form-urlencoded` and manually parse in php, is this corrent? ... http://stackoverflow.com/questions/35213197/xmlhttprequest-sending-empty-post –  Feb 19 '17 at 22:16
  • 1
    PHP doesn't natively understand JSON. If you just want to get it done, you can try something like `$_POST = json_decode(file_get_contents('php://input'), TRUE);` – Harry Pehkonen Feb 19 '17 at 22:17
  • this with the `application/json` or with the `application/x-www-form-urlencoded` - thanks for the help. –  Feb 19 '17 at 22:26
  • 1
    That gives PHP the ability to handle application/json (although it's quick and dirty -- could also check $_SERVER["REQUEST_METHOD"] and $_SERVER["CONTENT_TYPE"], and populate $_REQUEST). – Harry Pehkonen Feb 19 '17 at 22:31
  • seems kind of korky php doesn't have any built in automatic `application/json` content type parser on the server end... but for all your time, thanks! –  Feb 19 '17 at 22:35