Hosted extractor mapping language
The hosted extractor mapping language translates JSON structures to other JSON structures. The language is similar to JavaScript and is purely functional. A mapping is a single expression that describes how to build one JSON structure from another.
Variables
Use these input objects to define a mapping:
-
input
- This is the content of the received message, parsed into an object. -
context
- This contains information about the message. The content of this object varies between the different hosted extractors.For MQTT, the
context
object containstopic
: which topic the message arrived at
For examples of using these variables, see Handling more data in a single message.
When variables are objects or arrays they can be selected from. For example, given the object
{
"someField": 123,
"someArray": [1, 2, 3, { "nested": [1, 2, 3] }]
}
you can select from an object with
input.someField -> 123
or
input["someField"] -> 123
You select from arrays with
input.someArray[0] -> 1
Selectors can be nested as
input.someArray[3].nested[2] -> 3
Selectors can contain expressions
input.someArray[1 + 1] -> 3
Functions
The hosted extractor mapping language contains a set of predefined functions, which can be used to transform the input in other ways.
For example,
float("1.4") -> 1.4
will try to transform the input from a string to a number, which may fail.
Any function can also be called postfix, as a method on a value, so you could instead write
"1.4".float() -> 1.4
This is especially useful when dealing with chained operations.
See also Functions.
Lambda expressions
Some functions can take user defined functions as arguments, such as map
, reduce
, or filter
. To create the functions for these, use Lambda notation: argument => return
.
If the function takes no arguments, use ()
as the input to the function. If a function takes multiple values, denote them as a tuple: (arg1, arg2)
.
Examples
If we want to turn the list [1, 2, 3, 4]
into [2, 4, 6, 8]
we can use map
to apply a function to each member of the list, and write a lambda function that doubles each value:
[1, 2, 3, 4].map(number => number * 2) -> [2, 4, 6, 8]
If you want to remove all null
s from a list, use the filter
function, and write a lambda function that returns true if the value is the null
type using the is
operator:
[0, 1, 2, 3, 4, null, 5].filter(item => !(item is "null")) -> [0, 1, 2, 3, 4, 5]