When using FeedBlitz’s template personalization tags, you can access and dereference JSON data structures to help build custom content. The JSON data can be created on three ways:
- Stored as a FeedBlitz text custom field, or
- Delivered into the content via the <$BlogConnect$> template tag, or
- Created as a JSON array from stored text content via the “split” function.
Note: FeedBlitz’s JSON capabilities do not run to a full Javascript interpreter, and the data is read-only. You cannot alter, add to, or delete a JSON object within the template.
Accessing JSON elements
If you have a JSON object in the template variable “foo” then you can access any part of that JSON in the same way you would in any JSON-aware language. Some examples, for a hypothetical object describing a city:
- foo.city – the “city” object inside foo
- foo[“city”] – the city object inside foo
- foo.city.boroughs – an array of borough elements in the city
- foo.city.boroughs.length – the number of administrative subdivisions within the city
- foo.city.boroughs[2] – the third borough
- etc.
If your data is a string, say of preferences selected by the recipient, you can create a JSON array from that. Say you have a template variable “genres” which contains “sci-fi, horror, rom-com” – you can create a JSON array and assign it to a new variable “prefs” like this:
- <$eval prefs:=genres split “, ” $>, or
- <$eval prefs:=split(genres, “, “) $>
In this example, prefs.length is now 3, prefs[0] is “sci-fi”, etc.
split
Splits a string S separated by string P into a JSON array.
Usage
- S split P
- split(S,P)
Examples
- “10.0.0.1” split “.” – returns the JSON array [10,0,0,1]
- split(“red, green, #008, 0″ ,”, “) – returns the array [“red”, “green”, “#008”, 0]
join
Takes a JSON array A and creates a string representation of it, separated by string S.
Usage
- S split P
- split(S,P)
Examples
- [192, 168, 1, 1] join “.” – returns the string “192.168.1.1”
- join( [“red”, “green”, “#008″, 0], ” and “) – returns the string “red and green and #080 and 0”
length
Provides the number of elements of the JSON object J. If applied to a simple string S, returns the length of the string in characters.
Usage
- J length
- J.length
- length(J)
- S length
- S.length
- length(S)
Examples
- [1,2,3,4,5].length – returns 5 (the number of elements in the array)
- “[1,2,3,4,5]” length – returns 11 (the length of the string)
- length({“foo”:”1″, “bar”: “2”}) – returns 2.