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.
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.
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.
7 thoughts on “Safe JSON parsing in XPages SSJS”
“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.
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 🙂
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!
“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.
[…] 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 […]
[…] library https://github.com/douglascrockford/JSON-js/blob/master/json2.js –> <!– https://xomino.com/2013/12/18/safe-json-parsing-in-xpages-ssjs/ –> […]