# JsonPath Transformation Service

Transforms a JSON structure on basis of the JsonPath expression to an JSON containing the requested data.

# Examples

# Basic Example

Given the JSON

[{ "device": { "location": "Outside", "status": { "temperature": 23.2 }}}]

the JsonPath expression $.device.location exstracts the string instead a valid JSON [ "Outside" ], see differences.


the JsonPath expression $.device.status.temperature exstracts the number instead a valid JSON [ 23.2 ], see differences.


# In Setup


String  Temperature_json "Temperature [JSONPATH($.device.status.temperature):%s °C]" {...}
Number  Temperature "Temperature [%.1f °C]"


rule "Convert JSON to Item Type Number"
    Item Temperature_json changed
    // use the transformation service to retrieve the value
    val newValue = transform("JSONPATH", "$.device.status.temperature", Temperature_json.state.toString)

    // post the new value to the Number Item
    Temperature.postUpdate( newValue )

Now the resulting Number can also be used in the label to change the color or in a rule as value to compare.

# Differences to standard JsonPath

Compared to standard JSON the transformation it returns evaluated values when a single alement is retrieved from the query. Means it does not return a valid JSON [ 23.2 ] but 23.2, [ "Outside" ] but Outside. This makes it possible to use it in labels or output channel of things and get Numbers or Strings instead of JSON arrays. A query which returns multiple elements as list is not supported.

# Usage as a Profile

The functionality of this TransformationService can be used in a Profile on an ItemChannelLink too. To do so, it can be configured in the .items file as follows:

String <itemName> { channel="<channelUID>"[profile="transform:JSONPATH", function="<jsonPath>", sourceFormat="<valueFormat>"]}

The JSONPath expression to be used has to be set in the function parameter. The parameter sourceFormat is optional and can be used to format the input value before the transformation, i.e. %.3f. If omitted the default is %s, so the input value will be put into the transformation without any format changes.

Please note: This profile is a one-way transformation, i.e. only values from a device towards the item are changed, the other direction is left untouched.

# Further Reading