This section describes an example of using JSONPath().
If the sensor data (cv.Payload
) or the argument of the action to be executed in the trigger condition (tv.Data
) is in JSON format, you can use JSONPath() to refer to the value.
Sample Data
{ "store": {
"book": [
{ "category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{ "category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{ "category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{ "category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}
For example, if you want to refer to store -> bicycle -> color , use
cv.Payload.store.bicycle.color
But if you use JSONPath() you can write
JSONPath("$.store.bicycle.color", cv.Payload)
It can also be written as
The first argument of JSONPath path
path | Description |
---|---|
$ | root object/element |
@ | current object/element |
. or [] | child operator |
… | recursive descending |
* | wildcard |
[] | subscript operator (native array operator) |
[,] | union operator (alternative names or array indices can be used as sets) |
[start:end:step] | array slice operator |
?() | filter (script) expression |
() | Script expression using the underlying script engine |
Example of the first argument path of JSONPath
Example | Description |
---|---|
$.store.book[*].author | all authors in the books array in the store |
$..author | all authors |
$.store.* | the books array in the store and bicycle |
$.store..price | all prices in the store |
$..book [2] | the third book |
$..book[(@.length-1)]$..book[-1:] | last book |
$..book[0,1] $..book [:2] | first two books in book array |
$..book[?(@.isbn)] | all isbn in book array |
$..book[?(@.price<10)] | price less than 10 in book array |
$..* | all nodes/elements |
For more information on JSONPath, see the Goessner.net reference here.
Need more help with this?
Join our slack community for help