Skip to content

Commit 4b6d9ac

Browse files
committed
Improved NGSI transform example
1 parent b55bc53 commit 4b6d9ac

File tree

5 files changed

+52
-35
lines changed

5 files changed

+52
-35
lines changed

README.md

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -69,58 +69,45 @@ In the example, this JSON file is hosted at `https://raw.githubusercontent.com/a
6969
```json
7070
[
7171
{
72-
"query": "type='BasicVehicle' and latitude",
72+
"description": "Transformation from NGSI v2 models from 'Normalized response representation' (JSON-LD version partially supported) to 'Simplified representation'",
73+
"query": "type and *.value",
7374
"cases": {
74-
"true": "https://raw.githubusercontent.com/alexandrainst/node-red-contrib-json-multi-schema/master/examples/OldVehicleToVehicle.jsonata.js"
75+
"true": "https://raw.githubusercontent.com/alexandrainst/node-red-contrib-json-multi-schema/master/examples/NGSI-Normalised-to-keyValues.jsonata.js"
7576
}
7677
},
7778
{
78-
"query": "type='WeatherObserved' and latitude and longitude",
79+
"description": "Transformation from Cesva-TA120 to NGSI v2 NoiseLevelObserved in Simplified representation",
80+
"query": "type='Cesva-TA120' and NoiseLevelObserved",
7981
"cases": {
80-
"true": "https://raw.githubusercontent.com/alexandrainst/node-red-contrib-json-multi-schema/master/examples/OldWeatherObservedToVehicle.jsonata.js"
82+
"true": "https://raw.githubusercontent.com/alexandrainst/node-red-contrib-json-multi-schema/master/examples/Cesva-TA120-to-NoiseLevelObserved.jsonata.js"
8183
}
8284
}
8385
]
8486
```
8587

8688
### Example of JSONata transformation
8789

88-
In the example, this JSONata file is hosted at `https://raw.githubusercontent.com/alexandrainst/node-red-contrib-json-multi-schema/master/examples/OldVehicleToVehicle.jsonata.js`
90+
In the example, this JSONata file is hosted at [`Cesva-TA120-to-NoiseLevelObserved.jsonata.js`](https://raw.githubusercontent.com/alexandrainst/node-red-contrib-json-multi-schema/master/examples/Cesva-TA120-to-NoiseLevelObserved.jsonata.js).
8991

9092
```js
9193
{
92-
"id": id,
93-
"type": "Vehicle",
94-
"vehicleType": vehicleType,
95-
"category": [
96-
category1
97-
],
98-
"location": {
99-
"type": "Point",
100-
"coordinates": [
101-
longitude,
102-
latitude
103-
]
104-
},
105-
"name": name,
106-
"speed": speed,
107-
"cargoWeight": cargoWeight,
108-
"serviceStatus": serviceStatus,
109-
"serviceProvided": [
110-
serviceProvided1,
111-
serviceProvided2
112-
],
113-
"areaServed": areaServed,
114-
"refVehicleModel": refVehicleModel,
115-
"vehiclePlateIdentifier": vehiclePlateIdentifier
94+
"id": NoiseLevelObserved.id,
95+
"type": NoiseLevelObserved.type,
96+
"LAeq": NoiseLevelObserved.LAeq,
97+
"dateObservedFrom": NoiseLevelObserved.dateObserved,
98+
"dateObservedTo": NoiseLevelObserved.dateObserved,
99+
"location": NoiseLevelObserved.location
116100
}
101+
117102
```
118103

104+
A more advanced example of JSONata transformation can be found in [`NGSI-Normalised-to-keyValues.jsonata.js`](https://raw.githubusercontent.com/alexandrainst/node-red-contrib-json-multi-schema/master/examples/NGSI-Normalised-to-keyValues.jsonata.js).
105+
119106
### Example of transformation from command line
120107
The JSON input messages must each be on one single line, and wrapped into a Node-RED structure `{"payload":...}`
121108

122109
```sh
123-
echo '{"payload":{"id":"vehicle:WasteManagement:1","type":"BasicVehicle","vehicleType":"lorry","category1":"municipalServices","latitude":-3.164485591715449,"longitude":40.62785133667262,"name":"C Recogida 1","speed":50,"cargoWeight":314,"serviceStatus":"onRoute","serviceProvided1":"garbageCollection","serviceProvided2":"wasteContainerCleaning","areaServed":"Centro","refVehicleModel":"vehiclemodel:econic","vehiclePlateIdentifier":"3456ABC"}}' | \
110+
echo '{"payload":{"id":"TA120-T246177","type":"Cesva-TA120","NoiseLevelObserved":{"id":"TA120-T246177-NoiseLevelObserved-2018-09-17T07:01:09.000000Z","sonometerClass":"1","location":{"coordinates":[24.985891,60.274286],"type":"Point"},"measurand":["LAeq | 48.6 | A-weighted, equivalent, sound level"],"dateObserved":"2018-09-17T07:01:09.000000Z","LAeq":48.6,"type":"NoiseLevelObserved"}}}' | \
124111
node ./index.js json-multi-schema-transformer --transformsUrl='"https://raw.githubusercontent.com/alexandrainst/node-red-contrib-json-multi-schema/master/examples/smart-data-transforms.json"'
125112
```
126113

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,28 @@
1-
$merge($each($, function($v, $k) { $exists($v.value) ? { $k:$v.value } : $exists($v.object) ? { $k:$v.object }: { $k:$v } }))
1+
/**
2+
* JSONata for converting from NGSI Normalised format to Simplified representation with key-value pairs.
3+
*
4+
* Inspired by https://github.com/FIWARE/data-models/blob/master/tools/normalized2LD.py but reversed.
5+
*
6+
* @author Alexandre Alapetite <https://alexandra.dk/alexandre.alapetite>
7+
* @copyright Alexandra Institute <https://alexandra.dk> for the SynchroniCity European project <https://synchronicity-iot.eu> as a contribution to FIWARE <https://www.fiware.org>.
8+
* @license MIT
9+
* @date 2019-12-13 / 2019-12-17
10+
*/
11+
(
12+
$fixObj := function($v, $k) {
13+
/* Ensure ISO 8601 date format for minor syntax errors like missing timezone, or produce an error */
14+
$v.type = "DateTime" and $substring("" & $v.value, -1) != "Z" ? { $k: $fromMillis($toMillis($v.value)) } :
15+
{ $k: $v.value }
16+
};
17+
18+
/* Transform to Simplified structure */
19+
$merge($each($, function($v, $k) {
20+
$exists($v.value) ? $fixObj($v, $k) :
21+
$exists($v.object) ? { $k: $v.object } :
22+
{ $k: $v }
23+
}))
24+
25+
/* Replace (i.e. update, delete) some key names */
26+
~> | ** [createdAt] | {"dateCreated": createdAt}, ["createdAt"] |
27+
~> | ** [modifiedAt] | {"dateModified": modifiedAt}, ["modifiedAt"] |
28+
)

examples/OldVehicleToVehicle.jsonata.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/**
2+
* Legacy example of JSONata transformation.
3+
*/
14
{
25
"id": id,
36
"type": "Vehicle",

examples/smart-data-transforms.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
}
88
},
99
{
10-
"description": "Custom local example",
11-
"query": "type='BasicVehicle' and latitude",
10+
"description": "Transformation from Cesva-TA120 to NGSI v2 NoiseLevelObserved in Simplified representation",
11+
"query": "type='Cesva-TA120' and NoiseLevelObserved",
1212
"cases": {
13-
"true": "https://raw.githubusercontent.com/alexandrainst/node-red-contrib-json-multi-schema/master/examples/OldVehicleToVehicle.jsonata.js"
13+
"true": "https://raw.githubusercontent.com/alexandrainst/node-red-contrib-json-multi-schema/master/examples/Cesva-TA120-to-NoiseLevelObserved.jsonata.js"
1414
}
1515
}
1616
]

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@
4747
"start": "node ./index.js",
4848
"lint": "node ./node_modules/.bin/jshint *.js",
4949
"pretest": "npm run-script lint",
50-
"test": "printf '{\"payload\":{\"id\":\"vehicle:WasteManagement:1\",\"type\":\"BasicVehicle\",\"vehicleType\":\"lorry\",\"category1\":\"municipalServices\",\"latitude\":-3.164485591715449,\"longitude\":40.62785133667262,\"name\":\"C Recogida 1\",\"speed\":50,\"cargoWeight\":314,\"serviceStatus\":\"onRoute\",\"serviceProvided1\":\"garbageCollection\",\"serviceProvided2\":\"wasteContainerCleaning\",\"areaServed\":\"Centro\",\"refVehicleModel\":\"vehiclemodel:econic\",\"vehiclePlateIdentifier\":\"3456ABC\"}} \\n {\"payload\":{\"id\":\"vehicle:WasteManagement:1\",\"type\":\"BasicVehicle\",\"vehicleType\":\"lorry\",\"category1\":\"municipalServices\",\"latitude\":-3.164485591715450,\"longitude\":40.62785133667270,\"name\":\"C Recogida 1\",\"speed\":51,\"cargoWeight\":314,\"serviceStatus\":\"onRoute\",\"serviceProvided1\":\"garbageCollection\",\"serviceProvided2\":\"wasteContainerCleaning\",\"areaServed\":\"Centro\",\"refVehicleModel\":\"vehiclemodel:econic\",\"vehiclePlateIdentifier\":\"3456ABC\"}}' | node ./index.js json-multi-schema-transformer --transformsUrl='\"https://raw.githubusercontent.com/alexandrainst/node-red-contrib-json-multi-schema/master/examples/smart-data-transforms.json\"' | node ./index.js json-multi-schema-resolver --mappingsUrl='\"https://raw.githubusercontent.com/alexandrainst/node-red-contrib-json-multi-schema/master/examples/smart-data-models.json\"' | node ./index.js json-multi-schema-validator"
50+
"test": "printf '{\"payload\":{\"id\":\"TA120-T246177\",\"type\":\"Cesva-TA120\",\"NoiseLevelObserved\":{\"id\":\"TA120-T246177-NoiseLevelObserved-2018-09-17T07:01:09.000000Z\",\"sonometerClass\":\"1\",\"location\":{\"coordinates\":[24.985891,60.274286],\"type\":\"Point\"},\"measurand\":[\"LAeq | 48.6 | A-weighted, equivalent, sound level\"],\"dateObserved\":\"2018-09-17T07:01:09.000000Z\",\"LAeq\":48.6,\"type\":\"NoiseLevelObserved\"}}} \\n {\"payload\":{\"id\":\"TA120-T246183\",\"type\":\"Cesva-TA120\",\"NoiseLevelObserved\":{\"id\":\"TA120-T246183-NoiseLevelObserved-2018-09-17T07:01:15.000000Z\",\"sonometerClass\":\"1\",\"location\":{\"coordinates\":[24.9030921,60.161804],\"type\":\"Point\"},\"measurand\":[\"LAeq | 37.6 | A-weighted, equivalent, sound level\"],\"dateObserved\":\"2018-09-17T07:01:15.000000Z\",\"LAeq\":37.6,\"type\":\"NoiseLevelObserved\"}}}' | node ./index.js json-multi-schema-transformer --transformsUrl='\"https://raw.githubusercontent.com/alexandrainst/node-red-contrib-json-multi-schema/master/examples/smart-data-transforms.json\"' | node ./index.js json-multi-schema-resolver --mappingsUrl='\"https://raw.githubusercontent.com/alexandrainst/node-red-contrib-json-multi-schema/master/examples/smart-data-models.json\"' | node ./index.js json-multi-schema-validator"
5151
}
5252
}

0 commit comments

Comments
 (0)