Back

Convert Velocity request parameters to JSON

  • Created: Apr 24, 2014
  • Author: Nathan I. Keiter
  • Title: Lead Network Applications Programmer

Description

Update to my previous submission.  I added an if block so it doesn't break if there are no request parameters.  Please replace this text with the description from my previous submission, then delete the old one.

Code

${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}
${esc.hash}${esc.hash}
${esc.hash}${esc.hash} @title Convert Velocity request parameters to JSON
${esc.hash}${esc.hash}
${esc.hash}${esc.hash} @purpose Convert GET/POST vars into JSON Object using ONLY Velocity script
${esc.hash}${esc.hash}
${esc.hash}${esc.hash} @author nathan.keiter@gmail.com
${esc.hash}${esc.hash}
${esc.hash}${esc.hash} @notes This demo is optimized for readibility.  Output can be easily minified 
${esc.hash}${esc.hash} by removing whitespace and subtracting 1 instead of 2 from ${esc.dollar}jsonString.length()
${esc.hash}${esc.hash} I realize I could just write an OSGI ViewTool to do the conversion, but this 
${esc.hash}${esc.hash} might be helpful to people who aren't comfortable doing that.
${esc.hash}${esc.hash}
${esc.hash}${esc.hash} @example URL
${esc.hash}${esc.hash} www.example.com/test?field1=value1&field2=value2&field3=value3
${esc.hash}${esc.hash} 
${esc.hash}${esc.hash} @example output 
${esc.hash}${esc.hash} { "field3" : "value3", "field2" : "value2", "field1" : "value1" } 
${esc.hash}${esc.hash}
${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}${esc.hash}

${esc.hash}set( ${esc.dollar}jsonString = "" )
${esc.hash}set( ${esc.dollar}jsonObject = "{}" )

${esc.hash}${esc.hash} Retrieve the request parameter keys
${esc.hash}foreach ( ${esc.dollar}key in ${esc.dollar}request.getParameterMap().keySet() )

	${esc.hash}${esc.hash} Retrieve the request parameter value
	${esc.hash}set( ${esc.dollar}value = ${esc.dollar}request.getParameter( ${esc.dollar}key ) )

	${esc.hash}${esc.hash} json escape the value String
	${esc.hash}set( ${esc.dollar}value = ${esc.dollar}esc.javascript( ${esc.dollar}value ) )

    ${esc.hash}${esc.hash} jsonify the key->value property set
    ${esc.hash}set( ${esc.dollar}jsonProperty = "\"" + ${esc.dollar}key + "\" : " + "\"" + ${esc.dollar}value + "\", " )

    ${esc.hash}${esc.hash} Append to properties String
    ${esc.hash}set( ${esc.dollar}jsonString = ${esc.dollar}jsonString + ${esc.dollar}jsonProperty )

${esc.hash}end

${esc.hash}if ( ${esc.dollar}jsonString.length() > 0 )

	${esc.hash}${esc.hash} Trim off the last comma space from the properties String
	${esc.hash}${esc.hash} Wrap properties String into JSON Object notation {}
	${esc.hash}set( ${esc.dollar}jsonStringLength = ${esc.dollar}jsonString.length() - 2 )
	${esc.hash}set( ${esc.dollar}jsonObject = "{ " + ${esc.dollar}jsonString.substring( 0, ${esc.dollar}jsonStringLength ) + " }" )

${esc.hash}end

${esc.hash}${esc.hash} Output to page or pass into ViewTool as argument
${esc.dollar}jsonObject

Have you created code you'd like to share with the dotCMS community?

Share your code Become a Contributor