Overview
This blog helps to understand JSON parser in IIB and how to validate the incoming JSON message.
What is JSON?
JavaScript Object Notation is a lightweight plain text format used for data interchange. It’s a collection of Name-Value pair.
How IIB parses the JSON message?
In IIB JSON message realized as Object (Name-Value pair) and Array. IIB provides the feature called JSON domain. JSON parser and serializer process the messages below Data under JSON domain.
JSON parser converts the incoming bit stream into logical tree structure. It just validates the syntax of the incoming JSON message but it won’t validate the content/value of incoming message against any schema (Swagger.json). Because JSON modelling is not supported by IIB. Serializer converts the logical tree structure into bit stream.
Below picture describes the JSON logical tree structure created by JSON parser,
If syntax of the incoming JSON message is wrong, IIB sends the json parser error response as like below:
E.g.: BIP5705E: JSON parsing errors have occurred. : F:\build\S1000_slot1\S1000_P\src\DataFlowEngine\JSON\ImbJSONParser.cpp: 257: ImbJSONParser::parseLastChild: ComIbmWSInputNode: MF_JSON_POC#FCMComposite_1_1
BIP5701E: A JSON parsing error occurred on line 6 column 1. An invalid JSON character (UTF-8: ‘0x00000022’) was found in the input bit stream. The JSON parser was expecting to find one of the following characters or types: ‘”}”, “,”‘. The internal error code is ‘0x00000108’. : F:\build\S1000_slot1\S1000_P\src\DataFlowEngine\JSON\ImbJSONDocHandler.cpp: 550: ImbJSONDocHandler::onInvalidCharacter: ComIbmWSInputNode: MF_JSON_POC#FCMComposite_1_1</text>
Creating REST API
This section describes how to create REST API to validate the JSON message using xsd
STEP 1: Create REST API project and specify the API base path.
STEP 2: Define the JSON schema (Swagger.json) under Model Definitions. Model definition helps to create a JSON schema to define a structure of the JSON message.
JSON Schema will be created under OtherResources folder with the default name “swagger.json”.
JSON Schema:
{“swagger” : “2.0”,
“info” : {
“title” : “JSONoverHTTP”,
“version” : “1.0.0”,
“description” : “JSON meesage custom validation”
},
“paths” : {
“/customValidation/xsd” : {
“post” : {
“operationId” : “postXsd”,
“responses” : {
“200” : {
“description” : “The operation was successful.”
}},
“consumes” : [ “application/json” ],
“produces” : [ “application/json” ],
“description” : “Insert a xsd”,
“parameters” : [ {
“name” : “body”,
“in” : “body”,
“schema” : {
“$ref” : “#/definitions/PersonDetail”
},
“description” : “The request body for the operation”,
“required” : true
} ]
}
}
},
“basePath” : “/json_overhttp/v1”,
“definitions” : {
“PersonDetail” : {
“type” : “object”,
“properties” : {
“name” : {
“type” : “string”
},
“age” : {
“type”: “number”
},
“address” : {
“type” : “object”,
“properties” : {
“street” : {
“type” : “string”
},
“city” : {
“type” : “string”
},
“phoneNumber” : {
“type” : “number”,
“format” : “length=10”
}
}
},
“ValidationFlag” : {
“type” : “string”
}
},
“required” : [ “name” ]
}
}
}
STEP 3: Create a new resource.
STEP 4: Define the resource path and select the operation as post
STEP 5: click the subflow icon to implement conversion of JSON to XML and validation.
REST API Message flow will be created after doing above mentioned step .postXsd is a sub flow where the actual implementations are done.
STEP 6: Create an XSD as per the Swagger document under a shared library and name it as PersonDeatailSchema.xsd.
STEP 7: Refer the Library to REST API.
Right click the REST API Select à Manage Library references à choose the shared library in which you have created the PersonDeatailSchema.xsd schema.
STEP 8: Add mapping node in to subflow (postXsd) to convert JSON message to xml message.
STEP 9: select Swagger.JSON as input and PersonDeatailSchema.xsd as output format. This will convert the incoming JSON message to an XML.
STEP 10: After mapping node add a validator node. configure properties Domain as XMLNSC and Validation as content and value.
Note: If the XSD is created under library no need to refer explicitly in message model. At run time broker will take the right schema to validate the incoming message.
below is the postXsd Subflow,
Testing the Message Flow
In the below test util got error response back from the REST API as “The value “1234567B91” is not a valid value for the “phonetype” datatype”.
Phone number defined as data type: number as per the JSON schema and equally phone number defined as integer in the xsd schema definition.
After converting message from JSON to XML validator node validates the incoming message against the Schema definition. As PhoneNumber field has the string value as “1234567B91” in the incoming message so validator node throws the error
Conclusion
IIB doesn’t support JSON message model but it allows you to access and manipulate the JSON message. So, validation can be achieved by creating customized code.
Informative ..
Well Explained on how IIB interprets JSON messages.:-)
I am not able to map JSON elements to XML elements. After doing the mapping, data for XML elements are populating as blank. Can you please attach the detailed screenshots for mapping step by step(from JSON to XML) and with more clarity.