mapping-lib-cg-lib
v1.0.7
Published
Library to take and apply JSONata transformations to any incoming JSON
Downloads
8
Maintainers
Readme
jsonata-mapping-cg-lib
1. Introduction
This library has the purpose of apply JSONata query/transform expressions to any incoming JSON, weather is on base64 or as JSON.
This library could be used as part of a component based on Open Integration Hub (OIH) framework or any Nodejs development that requires applying JSONata query search or transformation.
The library can be installed from npm page with the next:
npm install jsonata-mapping-cg-lib
, npm i jsonata-mapping-cg-lib
or yarn install jsonata-mapping-cg-lib
Within "jsonata-mapping-cg-lib" two additional libraries are used: jsonata: This is the main library used for JSONata query/transform application. utils-nxg-cg: is a N3xGen library that is used to print the necessary logs and help with different functions to validate the payload.
The final result of the process will be the transformation or the query result from the established JSONata expression at the start of the application or flow in OIH, this data will be returned as JSON format:
[
{
"customer": "Joe Doe",
"book": "Structure and Interpretation of Computer Programs",
"due": "2016-12-05"
},
{
"customer": "Jason Arthur",
"book": "Compilers: Principles, Techniques, and Tools",
"due": "2016-10-22"
},
{
"customer": "Jason Arthur",
"book": "Structure and Interpretation of Computer Programs",
"due": "2016-12-22"
}
]
It is important to mention that before the method returns the information, it will be validated that the response if it is a valid
JSON
or validJSON Array
. If this is correct, the response will bereturned
, otherwise an exception will bethrown
and the flow or process willstop
. It is important to mention that forjsonata_expression
property is importan to send it as JSON format otherwise it will return error.
2. Methods explanation
2.1. transformCsvToJson
This method is used to apply query search or transformations to any incoming payload. By default there is no "jsonta_expressions" established. It is neccesary to send a jsonata expression.
3. Argument and result explanation
Args:
- In the applyJSONata method there is only one parameter necessary:
- jsonata_expression: This one parameter will be the query expression that is going to be applied to all incoming JSON, this parameter need to be passed as JSON format since it will be validated before.
- content(required): This parameter is the content that will be transformed or search on it, it could be on JSON format or base64.
- In the applyJSONata method there is only one parameter necessary:
Result: The final result will variate depending on the jsonata_expression is established at the start moment. And the incoming JSON as a payload. If the jsonata expression is the following:
{
"name": FirstName & " " & Surname,
"mobile": Phone[type = "mobile"].number
}
3. Examples
This part explains some examples of how to send the input parameters to the library and what the result would be. The following data could be a valid jsonata expression to use as to evaaluate some JSON payload.
The JSONata expression and JSON payload could be in base64 encode or plain JSON.
{
"jsonata_expression": "{\"name\": FirstName & \" \" & Surname,\"mobile\": Phone[type = \"mobile\"].number}"
}
The teting payload need to be on JSON format.
{
"FirstName": "Fred",
"Surname": "Smith",
"Age": 28,
"Address": {
"Street": "Hursley Park",
"City": "Winchester",
"Postcode": "SO21 2JN"
},
"Phone": [
{
"type": "home",
"number": "0203 544 1234"
},
{
"type": "office",
"number": "01962 001234"
},
{
"type": "office",
"number": "01962 001235"
},
{
"type": "mobile",
"number": "077 7700 1234"
}
],
"Email": [
{
"type": "office",
"address": [
"[email protected]",
"[email protected]"
]
},
{
"type": "home",
"address": [
"[email protected]",
"[email protected]"
]
}
],
"Other": {
"Over 18 ?": true,
"Misc": null,
"Alternative.Address": {
"Street": "Brick Lane",
"City": "London",
"Postcode": "E1 6RF"
}
}
}
And the following is the final result for the transform / query.
{
"name": "Fred Smith",
"mobile": "077 7700 1234"
}