Back

Parsing a JSON String using Jquery and Velocity

Description

If you have a field on a content type that stores a JSON string, you can parse the string using a combination of Velocity and JQuery.

JQuery provides a parseJSON function that receives a JSON string and returns a Javascript Object.

You can find more information on how to use this function on the JQuery website: http://api.jquery.com/jQuery.parseJSON/

Code

<script language="JavaScript">
##$content is your content object, and myJSONString is the field that stores the JSON string
var obj = jQuery.parseJSON('$content.myJSONString');
##You can get the values from the resulting Javascript object. 
##This example assumes the JSON string has a name value like this: {"name":"John"}
alert(obj.name);
</script>