JSON Parser for JEXL and Velocity

I’ve built a small extension to expose the JSON parser (org.json.simple.parser.JSONParser - pre-packaged with Rhythmyx 6.5.2 in json-simple.jar) functionality to Velocity templates. If anyone else is interested in this functionality, I’ve attached a zip file with the Ant deployment code, the Entensions.xml entry and the java library.

If you install this package, you should be able to do something like this in your velocity templates:

#set( $obj = $user.jsonTools.parse( '{"foo":["bar0","bar1","bar2"]}' ) )
$obj.foo.get(2)

and the resulting output should be:

bar2

This may not be readily useful, however I’m working on a control type which will allow extensible and dynamic form elements in the content editors and will save to the repository database in JSON format.

I’m not entirely certain why (perhaps it’s because I didn’t use the Ant script myself) but when I install the package, $user.jsonTools does not show up in the velocity editor auto-complete drop down menu. Even so, it still executes properly.

Correcting an error in the deploy.xml file.

Note: the jsonTools module still doesn’t appear in the auto-complete drop-down, even when you run the ant script / patch install tool.

Update:

Added annotations and new methods to the module.

Methods:

Object parse( String string )
parses a JSON-formatted string into either a JSONObject or JSONArray object that can be accessed as if it was a Map or List respectively.

String encode( Object object )
attampts to encode any Map or List (or any JSONAware implementing object) into a JSON string.

List [b]createArray/b
creates an empty JSONArray object that you can fill and encode as needed.

Map [b]createObject/b
creates an empty JSONObject object that you can fill and encode as needed.

All these methods will be available in the workbench’s auto-complete drop-down.

Note: This version also includes an update to the json_simple library

FYI, the install.sh script was built in DOS line endings and may not run until it is converted.

Thanks very much for this - it is quite useful for something I’m working on (parsing JSON strings from a web service), and I was able to get it working easily. I definitely didn’t want to write my own JSON parser :slight_smile:

I had to modify install.sh before running it. Converted it to Unix format (used dos2unix utility), as well as changing the value for RHYTHMYX_HOME, which was hardcoded. And bI am able to see the JSON tools in the JEXL auto-complete dropdown.

If you’re willing to share source code, I’d be interested in seeing how you implemented this.

Thanks again for sharing this extension!

-Kathleen