In this tutorial, let's look at how to process a complex JSON queryvia Siddhi Query Language and use extracted values from the JSON query in the streaming integration logic of the Siddhi Query Language.
At the end of this tutorial, you will know:
- How to extract the string value of a JSON corresponding to a given path using
- How to extract the object of a JSON corresponding to a given path using
- How to tokenize the given JSON based on a provided path and get the response as an object using the
- How to use a sink of the
filetype to write event data that is processed within Siddhi to files.
Let’s consider a scenario where Sam, the factory foreman receives an inventory list from the Sweet Factory for required stocks. This list is in JSON format. This JSON contains information on required materials, required quantities and corresponding suppliers for those materials.
Before you begin:
It is required for you to try out all rthe previous tutorials that cover how to create Siddhi applications and simulate events for them to process
To process the inventory list sent by supplier in JSON format, let's create a Siddhi application as follows:
- Access the Stream Processor Studio and start creating a new Siddhi application.
Let's enter a name and a description for your new Siddhi application as follows.
First, let's define the input stream you need in order to capture the inventory lists.
Now let's see how to split the input JSON into separate events for each material type. For example the sample JSON array provided above needs to be divided into three events for the material types
Sugaras shown below.
Event 1 Event 2 Event 3
"Foodies (pvt) Ltd",
"Sherry Flour Mills"
"Sweet Stores (pvt) Ltd",
"Don Bakery Needs",
"Busy Bee Distributors",
"Sweet Stores (pvt) Ltd",
"Honey Wholesale (pvt) Ltd"
To do this, you need to use the
json:tokenizeAsObjectfunction available in the
siddhi-execution-jsonextension. This function generates separate events for elements in the
To use this extension, include it ion the from clause as shown below:
The above single line is indicated as an error in the Stream Processor Studio. This is because you have not completed the query at this stage. This error no longer appears once you perform the next stepo and complete the query.
The first parameter of the function indicates the input JSON that you need to tokenize. The second parameter indicates the path that is used to tokenize.
After splitting the input json into separate events, let’s see how you can extract the values from each event.
You can extract the
materialNameas a string as follows.
Here, the first parameter
jsonElementis the return attribute name of the
$.materialNameis the path of the JSON from where the material name is extracted.If you want to extract elements from your JSON as boolean, double, float, int, or long values, the
siddhi-execution-json extensionprovides support to do so via
In your sample JSON, the supplier list is arrives as an array. Assume that the factory foreman needs to extract it as an object for further processing. You can do it as shown below.
Then you can extract all the required attributes from the input JSON and push that into the
In the above query, you have extracted required stock amount and its unit, and later concatenated those by specifying
str:concatas the siddhi execution string.
Now you need to further split the events in the
EachMaterialStreamso that a single event only contains the information relating to one supplier. Therefore, let's tokenize the
You are using the
json:tokenizefunction here instead of
json:tokenizeAsObjectbecause the supplier list does not have any further nesting and therefore, receiving the supplier name as a string is sufficient.
Now, let's create a file with the quotation request for each supplier. For this, you can use a sink of the
filetype and extract information from the
The sink configuration needs to be added with the stream definition of the
EachSupplierStreamstream as shown above to connect the stream to the sink. This enables the sink to get the events to be published from the