Signed-off-by: Kai Kreuzer <kai@openhab.org> |
||
---|---|---|
.. | ||
readme.md |
readme.md
id | label | title | type | description | since | install |
---|---|---|---|---|---|---|
jsonpath | JsonPath | JsonPath - Transformation Services | transform | Transforms a JSON structure on basis of the [JsonPath](https://github.com/jayway/JsonPath#jayway-jsonpath) expression to an JSON containing the requested data. | 2x | auto |
{% include base.html %}
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.
Outside
the JsonPath expression $.device.status.temperature
exstracts the number instead a valid JSON [ 23.2 ]
, see differences.
23.2
In Setup
Item
String Temperature_json "Temperature [JSONPATH($.device.status.temperature):%s °C]" {...}
Number Temperature "Temperature [%.1f °C]"
Rule
rule "Convert JSON to Item Type Number"
when
Item Temperature_json changed
then
// 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 )
end
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
Returns null
if the JsonPath expression could not be found.
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.
Further Reading
- An extended introduction can be found at W3School.
- As JsonPath transformation is based on Jayway using a online validator which also uses Jaway will give most similar results.