Xomino

Domino with the new improved X

Safe JSON parsing in XPages SSJS

Posted by mark roden on December 18, 2013

JSON is now ubiquitous in the world of JavaScript and the origins can be found a http://www.json.org/.

Douglas Crockford was very concerned about using eval() to convert strings to objects and so he created  json2.js which can be found here https://github.com/douglascrockford/JSON-js/blob/master/json2.js. If you look at the code there is a huge RegEx in the middle of it which purposefully ensures that there is no dangerous code which the eval statement is run on. You should read the comments in the code – very insightful !

All browsers (IE8+) now support JSON.stringify or JSON.parse by default and we no longer need these functions to be added as an external library.

I found however that XPages SSJS does not seem to recognize the JSON object. So I added the json2.js code to an SSJS library and then added it as a resource within my XPage.

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
	<xp:this.resources>
		<xp:script src="/json2.jss" clientSide="false"></xp:script>
	</xp:this.resources>
	<xp:repeat id="repeat1" rows="30" var="rep1">
		<xp:this.value><![CDATA[#{javascript:
			var temp ='[{"name": "marky"},{"name": "Billy"},{"name": "John"}]'
			return JSON.parse(temp)
		}]]>
	</xp:this.value>
		<xp:text escape="true" id="computedField1" value="#{javascript:rep1.name}"></xp:text>
		<hr />
	</xp:repeat>
</xp:view>

And with this I am now able to take a string of text – convert it safely to a JSON object and then use as the source for my repeat control.

json1

This message was inspired by Tim’s Tripcony’s blog post.

I love the idea of storing data as a JSON string inside of a single notes document field. This would move using Lotus Notes closer and closer to a modern NoSQL system, making applications more and more portable – love that idea.

However – this renders searching by field useless – which is usually one of the  requirements of an application.

About these ads

6 Responses to “Safe JSON parsing in XPages SSJS”

  1. “this renders searching by field useless”… not necessarily; it just alters your query syntax a bit:

    [JSON] CONTAINS “?firstName? : ?*marky*?”

    Granted, you have to either be darned precise about whitespace in your JSON or account for that in your query syntax, but in theory this should still work.

    If you skip the JSON approach entirely in favor of MIMEBean, you can not only store the same kind of hierarchical data you can with JSON, but it makes it fairly straightforward to design app-specific search algorithms that actually perform better than standard full-text search but also go beyond the primitive “well, it’s in there somewhere” strategy full-text searches have always used to something that caters to the actual business process driving the need to search in the first place. For one example of this, check out the watrCoolr project on OpenNTF.

    • MarkyRoden said

      Tim – yes I guess my point was that notes search it right out and therefore any indexing benefits.

      I like the MIMEBean approach in watrCoolr – but that really isn’t my cup of technical tea – I need to better understand how Mongo and CouchDB index their JSON stores maybe something can be gleaned from that :)

      thanks for the comment :)

  2. Why don’t just use the Xpages SSJS functions: toJson and fromJson? http://xpageswiki.com/web/youatnotes/wiki-xpages.nsf/dx/Work_with_ServerSide_JavaScript

    • MarkyRoden said

      Thanks Johann I did not know about that. Amazing since it has been around since 8.5.1 apparently.

      I will probably write a new JSON. function to use this then as I would rather the syntax was the same as the CSJS version for consistency.

      I really appreciate you taking the time to share thank you!

  3. 8b30b0 said

    “I love the idea of storing data as a JSON string inside of a single notes document field. This would move using Lotus Notes closer and closer to a modern NoSQL system, making applications more and more portable – love that idea.

    However – this renders searching by field useless – which is usually one of the requirements of an application.”

    I’m doing this in an application I’m building I store json in a field which I then use when presenting data in the browser and I store a bunch of fields in a document so that I can search for them.

  4. […] blog post about adding json2.js created a discussion on it’s use and Johann pointed out that there were already JSON parsing functions baked into XPages SSJS. Phil Riand added them back in […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: