Browse files

remove keys that have empty values returned from MySQL

  • Loading branch information...
1 parent 60dd436 commit e46de6ec0588ec6309f3123c8ba7c741c8a826da @BigBlueHat committed Nov 14, 2011
Showing with 10 additions and 1 deletion.
  1. +10 −1 sql2couchdb.php
@@ -84,7 +84,14 @@ function map($row, $json, $mysqlQuery)
foreach($json as $k => $v)
- $json[$k] = map($row, $v, $mysqlQuery);
+ $v = map($row, $v, $mysqlQuery);
+ if ($v === null)
+ {
+ unset($json[$k]);
+ continue;
+ } else {
+ $json[$k] = $v;
+ }
if($k == '_id')
$json[$k] = (string) $json[$k];
@@ -93,6 +100,8 @@ function map($row, $json, $mysqlQuery)
return castSQLToPHP(getColType($json, $mysqlQuery), $row[$json]);
+ else
+ return null;
$json = (object) $json;

2 comments on commit e46de6e

+1, though I wonder if this should really be done this way. If someone set their value to null on purpose (ie., it's not just a default col value), then their app code might be expecting null. Ie., they might be doing a === comparison for null somewhere in their app.

Being relational db folk I could easily see them doing something as strict as this.


BigBlueHat replied Nov 17, 2011

What I was after was some way to detect that you want the key to be dropped. In the JSON spec if the value of a key is "undefined" (a JS specific thing, obviously), then the key gets dropped. PHP's only got "null" so I overloaded it a bit (probably) to make it work like "undefined" in JS.

However, as you point out "null" is a valid JSON value, so this does need more thought/planning behind it.

Any ideas?

Please sign in to comment.