May 17

When I first mentioned JSONQuery, I actually posted the core functionality of a more comprehensive parser. While I tried to keep things simple by writing queries in JavaScript, the task at the time required a tiny slice of custom syntax. Because JSONQuery grew in size, and in order to keep things simple, I stripped out the custom bits before writing up the original post. In hindsight, I realize the original code may be more complex than it needs to be.

If JSONQuery accepts queries written in JavaScript, why should we waste time with a custom parser when JavaScript has a built-in parser, namely eval? That all depends on how you feel about eval. For example, Douglas Crockford famously frowns upon eval:

The eval function (and its relatives, Function, setTimeout, and setInterval) provide access to the JavaScript compiler. This is sometimes useful for parsing JSON text, but in virtually all other cases it indicates the presences of extremely bad coding. The eval function is the most misused feature of JavaScript.

JSONQuery is at its core a JavaScript parser. Since any JSON text has presumably already been parsed and converted into an Object, what we're actually building is a general purpose Object query. So, assuming you're comfortable with eval in this context, JSONQuery evolves into Object.query and becomes even simpler. Here is the new Object.query in action:

Object.prototype.query = function(query) {
  var result = eval('this.' + query);
  return !!result ? result : null;
};

var json = {
  'name': 'Bob',
  'address': [{
      'street': '1234 Main St.',
      'city' : 'New York',
      'state': 'NY',
      'zip': '10101'
    },{
      'street': '5678 First St.',
      'city' : 'San Francisco',
      'state': 'CA',
      'zip': '94126'
    }]
};

json.query('address[1].zip'); // 94126

Of course, there are still issues present in this minimalist implementation. Are we certain query is a String? Are we certain query is safe to eval? Are there other sanitizations we should perform? What can be done about any parse errors during eval? These are basic issues that can be easily corrected before putting Object.query into the wild.

These issues aside, the evolution of JSONQuery to Object.query has several benefits. It's much easier to understand what's happening. By its nature, eval is much faster than the original parser. Finally, it's more useful as a method present on any Object. All of this assumes, of course, that there is a need to retrieve the value of a specific property of an object at any given depth dynamically, using queries generated at run-time.

Post a comment

Basic XHTML (including links) is allowed, just don't try anything fishy. Your post will be auto-formatted unless you use your own <p> tags for formatting.

Please don't type anything here unless you're an evil robot:


And especially don't type anything here: