Active JSON concept

Well, these days it seems that everyone knows what JSON is and what it’s for. But, let me recap anyway.

JSON is a lightweight human-readable data-interchange format, a popular alternative to XML these days. So, why is it so popular? I believe, the main reason, is that it’s very simple – JSON is a subset of JavaScript (a tiny one), however it offers everything what’s required to properly represent structured data – it has strings, numbers and booleans to represent scalar data types, it has objects to represent sets of key-value pairs, it has arrays to represent vector data types and, of course, it allows combining all of those – Figure 1. Looks nice and complete, doesn’t it?

Plain JSON

Figure 1 - Plain JSON example

Like I mentioned before, JSON is a subset of JavaScript. So, what if we borrow one more data type fromJavaScript – function. What, if we also agreed that our function must return some value which, in turnmust be representable by JSON (consist of JSON-supported types). In this case our JSON could look like this:

Active JSON

Figure 2 - Active JSON example

As you probably noticed, we also stripped object keys form quotation marks wherever we could, to make code look even cleaner (JavaScript allows that if a key value is a valid JavaScript identifier)

What we got is something in between plain vanilla JSON and full-featured JavaScript.

Now, how do we process it? We may use some JavaScript engine (Mozilla Rhino for example) to compile such JSON in JavaScript object, then we execute the corresponding function, and finally, substitute the function with the result it returned. This process may be represented with following JavaScript codeFigure 3.

JavaScript representation of Active JSON processing

Figure 3 - JavaScript representation of Active JSON processing

Now variablejson points to JavaScript object equivalent of JSON displayed on Figure 1. All we have to do is to convert it back into JSON format.

I call this concept – Active JSON. It’s just a bit more complex then normal JSON, but a lot more dynamic. What’s the use of it? It’s not particularly useful for data transfer or persistence, I must admit, however it may be used to define JSON templates. All you need to do, is provide some context, while executing the dynamic part like shown on Figure 4.

Active JSON Processing

Figure 4 - Active JSON Processing

You might say: I could use any scripting language (JavaScript, php, Groovy, …) to generate JSON and get similar result. This is true, however, you would hardly get cleaner, simpler and shorter template code then with Active JSON.

If you want to see Active JSON in action, you may check out Restty – tool for accessing RESTful JSON-based Web Services that we built around it.

Share Button
Posted in General Tagged with: ,
2 comments on “Active JSON concept
  1. Steven Walker says:

    Has anyone added any NTLM authentication capability for restty?

2 Pings/Trackbacks for "Active JSON concept"
  1. […] protocol utilized by Google in theirs APIs. Even better, Restty also includes full set of Active JSON templates for accessing Google Tasks and URL Shortener APIs (see gtasks and gshort […]

Leave a Reply

Your email address will not be published. Required fields are marked *