How to create Concept instances from JSON payloads with no root element

How to create Concept instances from JSON payloads with no root element

book

Article ID: KB0072861

calendar_today

Updated On:

Products Versions
TIBCO BusinessEvents Enterprise Edition 6.1 and later

Description

In some cases, the BE application needs to consume messages (e.g. EMS, Kafka) with JSON payloads which have no root element. For example:
 
 {   "BookTitle":"Finnegans Wake",   "BookAuthor":"James Joyce",   "BookPrice":24.98 }

Refer to the steps below to consume these messages in BE using the createInstanceFromJSON() function.
 

Issue/Introduction

Outlines the steps needed to create new Concept instances from JSON payloads that have no root element.

Environment

All Supported Platforms

Resolution

First, configure the Payload in the Event definition with Content = Any Element:

any element event payload

Next, define properties for the Concept that will be instantiated when a new message arrives:

BookData concept properties

Next, create a RuleFunction to serve as a pre-processor for the incoming messages, and use the createInstanceFromJSON() function. For example:

 
 void rulefunction RuleFunctions.PreProc {   attribute {     validity = ACTION;   }   scope {     Events.BookDataEvent bookdataevent;   }   body {     System.debugOut("*** Instantiating BookData Concept ****");     Concepts.BookDataConcept bdInst = Instance.createInstanceFromJSON("/Concepts/BookDataConcept",bookdataevent@payload);     System.debugOut("*** New BookDataConcept: "+bdInst.BookTitle         +" by " + bdInst.BookAuthor + " ("+bdInst.BookPrice+")");   } }

Finally, at either the cluster or processing-unit level, set the property 'be.http.json.rootElement.ignore' to 'true':

ignore root element

You should see the following message emitted to the console:

 
 2021 Nov 19 06:58:45:995 GMT -05 dev.machine INFO [$default.be.mt$.Thread.7] - [user] [jms]  *** New BookDataConcept: Finnegans Wake  by James Joyce (24.98)