Skip to main content
Kinetic Community

JSON Tag Library

The Kinetic JSON tag library is intended to make working with JSON objects and arrays easier.

Getting Started

Configuration

The JSON tag library can be declared with the following taglib directive:

<%-- bundle/initialization.jspf --%>
<%@taglib prefix="json" uri="http://kineticdata.com/taglibs/json" %>

Usage

<%-- form.jsp --%>
<json:object>
  ...
</json:object>

Tags

The following tags are provided in the JSON tag library:

<json:property>

You use the json:property tag to render a JSON property. This will add a property to the data, object or array tag that the json:property tag is nested within. JSON properties are basically just name/value pairs. The name is always a string and is set with the name="..." attribute on the tag.

There are a couple of ways you can specify the value that is set for a JSON property.

  • Within the body of the tag. Any content that is rendered within the tag body will be assumed to be a string.

  • By setting the value="..." attribute on the tag. If the value specified is a Boolean, then it will be converted to a JSON boolean. If the value specified is a number (Integer, Short, Long, Double, Float) then it will be converted to a JSON numeric value. If the value is a String it will be converted to a JSON string. Any other Java types that are used to set the value will have toString() called on them and they will be treated as JSON strings.

Attributes

  • name (optional) name of the property.
  • value (optional) value of the property.

Example

<json:property name="bool1" value="${true}"/>

will produce

"bool1": true

<json:array>

The json:array tag is used to create JSON arrays. A JSON array may consist of Strings, numeric values, booleans, JSON objects, or further JSON arrays.

There are a few ways you can interact with the json:array tag to create your data.

  • Pass a collection or array of values using the items="..." attribute. The collection will be iterated over, and each value in the collection will be added to the JSON Array. The type of the items in the collection follows the same rules as for JSON Properties, so Integers will become JSON numeric, Booleans will become JSON booleans and so on.
  • Pass a collection or array of values using the items="..." attribute, and specify a var="..." attribute that you then use in the tag body.
  • Don’t specify any items collection, and render the array elements individually. You can render the items individually, or use JSTL and the c:forEach tag to iterate over a collection in whatever way you wish within the tag, adding elements to the array.

Attributes

  • name (optional) sets the resulting array to a property named as the value of this attribute. Required only when the json:array is a child of a json:object tag.
  • items (optional) a collection of items to automatically generate the values
  • var (optional) Name of the variable referenced by each iteration of the items attribute.

Example

<json:object>
    <json:array name="days" items="${days}"/>
</json:object>

will produce

{
  "days": ["Mon","Tue","Wed","Thur","Fri","Sat","Sun"]
}

<json:object>

The json:object tag is used to create JSON objects. Objects can be nested as many times as you want. JSON objects contain properties, added using the json:property tag.

Attributes

  • name (optional) sets the resulting object to a property named as the value of this attribute. Required only when the json:object is a child of a json:object tag.

Example

<json:object>
  <json:property name="outer" value="foo"/>

  <json:object name="inner">
    <json:property name="innerProp1" value="val1"/>
    <json:property name="innerProp2" value="val2"/>

    <json:object name="inner2">
      <json:property name="inner2Prop1" value="in2p1"/>
      <json:property name="inner2Prop2" value="in2p2"/>
    </json:object>  

  </json:object>
</json:object>

will produce

{
  "outer": "foo",
  "inner": {
    "innerProp1": "val1",
    "innerProp2": "val2",
    "inner2": {
      "inner2Prop1": "in2p1",
      "inner2Prop2": "in2p2"
    }
  }
}

Related