Skip to content

API Docs - v4.0.20

Sinkmapper

json (Sink Mapper)

Event to JSON output mapper.
Transports which publish messages can utilize this extensionto convert the Siddhi event to JSON message.
Users can either send a pre-defined JSON format or a custom JSON message.

Syntax

@sink(..., @map(type="json", validate.json="<BOOL>", enclosing.element="<STRING>")

QUERY PARAMETERS

Name Description Default Value Possible Data Types Optional Dynamic
validate.json This property will enable JSON validation for generated JSON message.
By default value of the property will be false.
When enabled, DAS will validate the generated JSON message and drop the message if it does not adhere to proper JSON standards.
BOOL No No
enclosing.element Used to specify the enclosing element in case of sending multiple events in same JSON message.
WSO2 DAS will treat the child element of given enclosing element as events and execute json expressions on child elements.
If enclosing.element is not provided multiple event scenario is disregarded and json path will be evaluated with respect to root element.
STRING No No

Examples EXAMPLE 1

@sink(type='inMemory', topic='stock', @map(type='json'))
define stream FooStream (symbol string, price float, volume long);

Above configuration will do a default JSON input mapping which will generate below output.
{
    "event":{
        "symbol":WSO2,
        "price":55.6,
        "volume":100
    }
}

EXAMPLE 2

@sink(type='inMemory', topic='{{symbol}}', @map(type='json', enclosing.element='$.portfolio', validate.json='true', @payload( "{"StockData":{"Symbol":"{{symbol}}","Price":{{price}}}")))
define stream BarStream (symbol string, price float, volume long);

Above configuration will perform a custom JSON mapping which will produce below output JSON message
{"portfolio":{
    "StockData":{
        "Symbol":WSO2,
        "Price":55.6
      }
  }
}

Sourcemapper

json (Source Mapper)

JSON to Event input mapper. Transports which accepts JSON messages can utilize this extensionto convert the incoming JSON message to Siddhi event. Users can either send a pre-defined JSON format where event conversion will happen without any configs or can use json path to map from a custom JSON message.

Syntax

@source(..., @map(type="json", enclosing.element="<STRING>", fail.on.missing.attribute="<BOOL>")

QUERY PARAMETERS

Name Description Default Value Possible Data Types Optional Dynamic
enclosing.element Used to specify the enclosing element in case of sending multiple events in same JSON message.
WSO2 DAS will treat the child element of given enclosing element as events and execute json path expressions on child elements.
If enclosing.element is not provided multiple event scenario is disregarded and json path will be evaluated with respect to root element.
STRING No No
fail.on.missing.attribute This can either have value true or false. By default it will be true.
This attribute allows user to handle unknown attributes.
 By default if an json execution fails or returns null DAS will drop that message.
However setting this property to false will prompt DAS to send and event with null value to Siddhi where user can handle it accordingly.
(ie. Assign a default value)
BOOL No No

Examples EXAMPLE 1

@source(type='inMemory', topic='stock', @map(type='json'))
define stream FooStream (symbol string, price float, volume long);

Above configuration will do a default JSON input mapping
. For a single event, expected input will look like below.
{
    "event":{
        "symbol":"WSO2",
        "price":55.6,
        "volume":100
    }
}

EXAMPLE 2

@source(type='inMemory', topic='stock', @map(type='json'))
define stream FooStream (symbol string, price float, volume long);

Above configuration will do a default JSON input mapping.
For multiple events, expected input will look like below.
[
{"event":{"symbol":"WSO2","price":55.6,"volume":100}},
{"testEvent":{"symbol":"WSO2","price":56.6,"volume":99}},
{"event":{"symbol":"WSO2","price":57.6,"volume":80}}
]

EXAMPLE 3

@source(type='inMemory', topic='stock', @map(type='json', enclosing.element="$.portfolio", @attributes(symbol = "company.symbol", price = "price", volume = "volume")))

Above configuration will perform a custom JSON mapping.
For a single event, expected input will look like below
.{
 "portfolio":{
     "stock":{ "volume":100,
        "company":{
           "symbol":"WSO2"
       },
        "price":55.6
    }
}

EXAMPLE 4

@source(type='inMemory', topic='stock', @map(type='json', enclosing.element="$.portfolio", @attributes(symbol = "stock.company.symbol", price = "stock.price", volume = "stock.volume")))
define stream FooStream (symbol string, price float, volume long);

Above configuration will perform a custom JSON mapping.
For multiple events, expected input will look like below
.{"portfolio":
   [ {"stock":{"volume":100,"company":{"symbol":"wso2"},"price":56.6}}, {"stock":{"volume":200,"company":{"symbol":"wso2"},"price":57.6}} ]
}