add a function json_valid which returns true if the argument is valid json

I found that my queries were incomplete unless I could differentiate between an invalid object and an object that didn't have the property I was looking for.

The code could probably be simplified; I welcome any feedback you may have.