Transform JSON
Provides examples on transforming JSON object within Siddhi.
For all functions available to transform JSON see Siddhi Execution JSON.
define stream InputStream(jsonString string);
from InputStream
select json:toObject(jsonString) as jsonObj
insert into PersonalDetails;
from PersonalDetails
select jsonObj,
json:getString(jsonObj,'$.name') as name,
json:isExists(jsonObj, '$.salary') as isSalaryAvailable,
json:toString(jsonObj) as jsonString
insert into OutputStream;
from OutputStream[isSalaryAvailable == false]
select
json:setElement(jsonObj, '$', 0f, 'salary') as jsonObj
insert into PreprocessedStream;
|
|
|
|
|
Transforms JSON string to JSON object which can then be manipulated |
|
|
|
|
|
Get the |
|
Validate if |
|
Stringify the JSON object |
|
|
|
|
|
Set |
|
Input
Below event is sent to InputStream
,
[
{
"name" : "siddhi.user",
"address" : {
"country": "Sri Lanka",
},
"contact": "+9xxxxxxxx"
}
]
Output
After processing, the following events will be arriving:
- OutputStream:
[{"address":{"country":"Sri Lanka"},"contact":"+9xxxxxxxx","name":"siddhi.user"}
,siddhi.user
,false
,"{\"name\" : \"siddhi.user\", \"address\" : { \"country\": \"Sri Lanka\", }, \"contact\": \"+9xxxxxxxx\"}"
] PreprocessedStream:
[{ "name" : "siddhi.user", "salary": 0.0 "address" : { "country": "Sri Lanka", }, "contact": "+9xxxxxxxx" }
]