Iterate through JSON arrays to receive a value from a given JSON element
- Last UpdatedJul 24, 2025
- 1 minute read
- PI System
- PI Connector for UFL 1.3.2.139
- Connectors
The JSON data format has many advantages such as that it is succinct, typed and straightforward. However, for UFL, which has been optimized for working with matrixes, the extraction logic without the looping support, was often unwieldy and cumbersome. The syntax got simpler with the statement. Users can locate a certain element. If this element is an array or a collection of similarly looking JSON objects, the construct allows for stepping through these sets, extracting the individual items and processing them.
Example 1
This example iterates through a JSON array and forwards the extracted variables to the PI System employing the and functions.
Note: The whole JSON structure is forwarded to the connector JSON parsing engine through the __MESSAGE variable.
{
"UpdateTime":"2017-04-30 23:58:10",
"VariableList": [
{ "id": "10240", "val":123.5},
{ "id": "10241", "val":83.1},
{ "id": "10242", "val":338.0}
]
}
TimeStamp = JsonGetValue(__MESSAGE, "UpdateTime")
FOREACH (JsonGetItem(__MESSAGE, "VariableList[]")) DO
ID = JsonGetValue(__ITEM, "id")
Value_Number = JsonGetValue(__ITEM, "val")
StoreEvent(ID, ,TimeStamp, Value_Number)
ENDFOR
Example 2
This example iterates through a "bare" JSON array, using the selector, and prints out individual array items.
["VW","BMW","Fiat"]
FOREACH (JsonGetItem(__MESSAGE, "[]")) DO
Print(__ITEM)
ENDFOR