Save boolean data types in serialized array I'm writing a CRUD front end application that talks to a hand-written Drupal endpoint. The data I'm pulling from the database is serialized in one field, or cell. It comes out as a keyed array. 

One of the actions a user can do is to set a value true/false. The problem I'm running into is that when I send that data to the database from Javascript, it's coming in as a string:

 request.post('/my-api/save/' + id )
  .set('Content-Type', 'application/json')
  .send('{"' + prop + '":"' + val + '"}')


And when it's serialized and saved the the database val is written as a string. The problem is when val it is equal to "false." I'm using drupal_write_record to save the array. When this comes back to the front end, it's still a string, which in Javascript gets coerced to true. 

What I'm struggling with is how/where to turn that string into a boolean. I'm guessing I should be doing this on the save action, but maybe there's a Drupal specific way to do this.

I'm doing this to the incoming json at the moment, I don't like it but it works for now.

$received_json = file_get_contents("php://input",  TRUE);
$json = drupal_json_decode($received_json);
foreach($json as $key =&gt; $val) {
  // Typecast the "false" string
  $json[$key] = $val === 'false' ? (bool) FALSE : $val;
}
This would be much easier to do on the client side. Assuming val is your boolean value, you can just add a single line to handle this:

val = +val;


That will coerce your boolean to an integer (1 or 0). No need to check all of the json data on the server side.