Allows content to be processed before any additional formatting is applied. Once processed other options such as cleansing, prefix, suffix are applied. Only fields with content are processed. This option should only be used on fields that do not update back to a data source, because there is no way of reversing the process. Ensure that preprocessing is stand-alone and not reliant on the formatted content of other fields as they may not exist at the point the script is called.
The following methods are built into EasyCatalog:
Lua
The content is passed as a ‘content’ variable and the return value is used as the new content. For example:
1 | return string.format("<b>%s</b>", content) |
Parsing JSON into text using jsoncursor:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | -- available in 2023 beta and later local function summarize(cursor) local result = ""; if cursor == nil then return result end indentlevel = 0; while not cursor:done() do local event_type = cursor:type() if event_type == "beginarray" or event_type == "beginobject"then indentlevel = indentlevel + 1; cursor:next() elseif event_type == "endobject" or event_type == "endarray" then indentlevel = indentlevel - 1; cursor:next() elseif event_type == "key" then result = result .. string.rep(" ", indentlevel - 1) result = result .. cursor:getstring() .. " : "; cursor:next() local event_type = cursor:type() if event_type == "beginarray" or event_type == "beginobject" then result = result .. "\n" else result = result .. tostring(cursor:getvalue()); result = result .. "\n" cursor:next() end else result = result .. string.rep(" ", indentlevel - 1) result = result .. tostring(cursor:getvalue()); result = result .. "\n" cursor:next() end end return result end return summarize(jsoncursor(content)) |
Mustache
Mustache is a logicless template engine for creating dynamic content from JSON fields.
A typical Mustache template:
1 2 3 4 5 6 | Hello {{name}} You have just won {{value}} dollars! {{#in_ca}} Well, {{taxed_value}} dollars, after taxes. {{/in_ca}} |
Given the following field content:
1 2 3 4 5 6 7 | { "name": "Chris", "value": 10000, "taxed_value": 6000, "in_ca": true } |
Will produce the following:
1 2 3 4 | Hello Chris You have just won 10000 dollars! Well, 6000.0 dollars, after taxes. |
Here’s another example, which uses a template to create a HTML table which can then be loaded as Tabular Field. Original JSON data:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | { "Specs": [ { "SpecsUniqueName": "AmpOutPower8", "SpecsCaption": "Ouput Power (8 Ohms)", "SpecsValue": "2x300W", "SpecsUnit": "", "SpecsAlternativeCaption": "", "SpecsAbbriviation": "", "SpecsFeatureHelp": "" }, { "SpecsUniqueName": "AmpOutPower4", "SpecsCaption": "Ouput Power (4 Ohms)", "SpecsValue": "2x500W", "SpecsUnit": "", "SpecsAlternativeCaption": "", "SpecsAbbriviation": "", "SpecsFeatureHelp": "" }, { "SpecsUniqueName": "AmpOutPowerBridge8", "SpecsCaption": "Ouput Power Bridge (8 Ohms)", "SpecsValue": "1000W", "SpecsUnit": "", "SpecsAlternativeCaption": "", "SpecsAbbriviation": "", "SpecsFeatureHelp": "Rated ouput power (1kHz @ 8 Ohms, bridged)" }, { "SpecsUniqueName": "AmpOutPowerBridge4", "SpecsCaption": "Output Power Bridge (4 Ohms)", "SpecsValue": "1300W", "SpecsUnit": "", "SpecsAlternativeCaption": "", "SpecsAbbriviation": "", "SpecsFeatureHelp": "" }, { "SpecsUniqueName": "AmpSignalNoiseRatio", "SpecsCaption": "Signal to Noise Ratio", "SpecsValue": ">102bD", "SpecsUnit": "", "SpecsAlternativeCaption": "", "SpecsAbbriviation": "", "SpecsFeatureHelp": "" }, { "SpecsUniqueName": "AmpSignalNoiseRatio", "SpecsCaption": "Signal to Noise Ratio", "SpecsValue": ">102dB", "SpecsUnit": "", "SpecsAlternativeCaption": "", "SpecsAbbriviation": "", "SpecsFeatureHelp": "" }, { "SpecsUniqueName": "AmpSlewRate", "SpecsCaption": "Slew Rate", "SpecsValue": "40V/uS", "SpecsUnit": "", "SpecsAlternativeCaption": "", "SpecsAbbriviation": "", "SpecsFeatureHelp": "" }, { "SpecsUniqueName": "AmpInputImpedance", "SpecsCaption": "Input Impedance", "SpecsValue": "10K Ω", "SpecsUnit": "", "SpecsAlternativeCaption": "", "SpecsAbbriviation": "", "SpecsFeatureHelp": "" }, { "SpecsUniqueName": "AmpOutputConnectors", "SpecsCaption": "Output Connectors", "SpecsValue": "4 Pole SPEAKON", "SpecsUnit": "", "SpecsAlternativeCaption": "", "SpecsAbbriviation": "", "SpecsFeatureHelp": "" }, { "SpecsUniqueName": "AmpOutputConnectors", "SpecsCaption": "Output Connectors", "SpecsValue": "Binding Post", "SpecsUnit": "", "SpecsAlternativeCaption": "", "SpecsAbbriviation": "", "SpecsFeatureHelp": "" }, { "SpecsUniqueName": "AmpInputConnectors", "SpecsCaption": "Input Connectors", "SpecsValue": "1/4\" (6.35mm)", "SpecsUnit": "", "SpecsAlternativeCaption": "", "SpecsAbbriviation": "", "SpecsFeatureHelp": "" }, { "SpecsUniqueName": "AmpCoolingSystem", "SpecsCaption": "Cooling System", "SpecsValue": "Front-to-Back via 2 variable-speed fans", "SpecsUnit": "", "SpecsAlternativeCaption": "", "SpecsAbbriviation": "", "SpecsFeatureHelp": "" }, { "SpecsUniqueName": "AmpPowerSupplyFuse", "SpecsCaption": "Power Supply Fuse", "SpecsValue": "8A", "SpecsUnit": "", "SpecsAlternativeCaption": "", "SpecsAbbriviation": "", "SpecsFeatureHelp": "" } ] } |
And the template:
1 2 3 4 5 6 7 8 9 | <table> {{#Specs}} <tr> <td>{{SpecsUniqueName}}</td> <td>{{SpecsCaption}}</td> <td>{{SpecsValue}}</td> </tr> {{/Specs}} </table> |
JMESPath
JMESPath (JSON Matching Expression Paths) is a query language for search JSON. It allows you to declaratively extract elements from JSON. For example:
1 | locations[?state == 'WA'].name | sort(@) | join(', ', @) |
Given the following field content:
1 2 3 4 5 6 7 8 | { "locations": [ {"name": "Seattle", "state": "WA"}, {"name": "New York", "state": "NY"}, {"name": "Bellevue", "state": "WA"}, {"name": "Olympia", "state": "WA"} ] } |
Will produce the following:
1 | "Bellevue, Olympia, Seattle" |
XPath
XPath can be used to navigate through elements and attributes of an XML field. Given the following field content:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <?xml version="1.0" encoding="UTF-8"?> <bookstore> <book category="cooking"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="children"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="web"> <title lang="en">XQuery Kick Start</title> <author>James McGovern</author> <author>Per Bothner</author> <author>Kurt Cagle</author> <author>James Linn</author> <author>Vaidyanathan Nagarajan</author> <year>2003</year> <price>49.99</price> </book> <book category="web"> <title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore> |
The XPath:
1 | /bookstore/book[price>35.00]/title |
Would give the result:
1 2 | XQuery Kick Start Learning XML |
JSONPath
JSONPath is a query language for JSON, similar to XPath for XML. It allows you to select and extract data from a JSON document.
JSONPath expressions begin with the dollar sign ($) character, which refers to the root element. The dollar sign is followed by a sequence of child elements, which are separated via dot (code) notation or via the square brackets (code).
The important JSONPath syntax rules are:
- $ symbol refers to the root object or element.
- @ symbol refers to the current object or element.
- . operator is the dot-child operator, which you use to denote a child element of the current element.
- [ ] is the subscript operator, which you use to denote a child element of the current element (by name or index).
- * operator is a wildcard, returning all objects or elements regardless of their names.
- , operator is the union operator, which returns the union of the children or indexes indicated.
- : operator is the array slice operator, so you can slice collections using the syntax [start:end:step] to return a subcollection of a collection.
- ( ) operator lets you pass a script expression in the underlying implementation’s script language. It’s not supported by every implementation of JSONPath, however.
- ? ( ) to query all items that meet a certain criteria.
- Take this example JSON:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | { "people": [ { "name": "Alice", "age": 30 }, { "name": "Bob", "age": 25 }, { "name": "Charlie", "age": 35 } ] } |
$.people[?(@.name == ‘Bob’)].age