Friday, March 6, 2009

Taking advantage of JSON from views in XPages

Since we're blessed with pseudo-JavaScript in XPages, it's time to make lookup-views with JSON columns.

If you have a well formed JSON-string, it's extremely simple to convert it to a JavaScript object.

Example:
var jsonString = '{"key":"value", "keyForNumber":2, "keyForArray":["first", "second"]}';
var jsonObject = eval( '(' + jsonString + ')' );

Result:
jsonObject = {
    key: "value",
    keyForNumber: 2,
    keyForArray: ["first", "second"]
}

Instead of making xx amount of views, have a couple of lookup-views. In the XPages, convert the JSON-string from a columnvalue to a JavaScript-object, then pick the values you need.

I've made a little demoapp of how you can take advantage of this with XPages:
>> Download

As I'm addicted to templating, I've made a simple templating-demo as well. Take a look at server side JS.

If you're lazy like me, also take a look at the (JSON\DiscworldCharacter)-view, in the lookup column.

1 comments:

Maks said...

Thanks for a interesting example. Very usefull!