Friday, 6 October 2017

ESQL Code Snippets

1. Convert XMLNSC to BLOB 

Sometimes you may need to convert your payload to a BLOB(Binary Large Object). To be able to accomplish this the ASBITSTREAM function has to be used. The ASBITSTREAM function will return a bit stream representation of your payload. When I need to put my payload to MQ queue I convert it to a BLOB.
 DECLARE myChar CHAR CAST(myBLOB AS CHAR CCSID InputRoot.Properties.CodedCharSetId Encoding InputRoot.Properties.Encoding);  

2. Convert BLOB to CHAR

 DECLARE myChar CHAR CAST(myBLOB AS CHAR CCSID InputRoot.Properties.CodedCharSetId Encoding InputRoot.Properties.Encoding);  

3. Convert CHAR to BLOB

 DECLARE myBlob BLOB CAST( myChar AS BLOB CCSID InputRoot.Properties.CodedCharSetId);  

4. Convert BLOB to XMLNSC 

 CREATE LASTCHILD OF OutputRoot.XMLNSC DOMAIN('XMLNSC') PARSE(myBlob, InputRoot.Properties.Encoding, InputRoot.Properties.CodedCharSetId);  

5. Left Padding

 RIGHT('0000000000' || CAST(field AS CHAR),10);  
The above will left zero pad field to a 10 long field.


6. Nil Element & Namespace Declaration

 SET OutputRoot.XMLNSC.ns:myElement.(XMLNSC.NamespaceDecl)xmlns:"xsi" ='http://www.w3.org/2001/XMLSchema-instance';    
 SET OutputRoot.XMLNSC.ns:myElement.(XMLNSC.Attribute)xsi:nil = 'true';   

7. Convert Payload to a String

 DECLARE myBlob BLOB;  
 SET myBlob = ASBITSTREAM(InputRoot.XMLNSC CCSID InputRoot.Properties.CodedCharSetId ENCODING InputRoot.Properties.Encoding);  
 DECLARE myChar CHAR CAST(myBlob AS CHAR CCSID InputRoot.Properties.CodedCharSetId Encoding InputRoot.Properties.Encoding);  

8. Backup a JSON Payload without losing arrays

 CREATE LASTCHILD OF OutputRoot DOMAIN('JSON') TYPE Name NAME 'JSON';  
 CREATE FIELD OutputRoot.JSON.Data IDENTITY(JSON.Object)Data;  
 SET OutputRoot.JSON.Data = InputLocalEnvironment.Backup.Data;   

9.Pass Parameters to an HTTP request Node 

 OutputLocalEnvironment.Destination.HTTP.QueryString.param1 = 'param1';  
Each parameter must be individually set.

10. Convert BLOB to JSON 

 CREATE LASTCHILD OF OutputRoot DOMAIN('JSON') PARSE(InputRoot.BLOB.BLOB);  

11. Select value from an ESQL array 

This statement allows you to select the name without iterating through OutputLocalEnvironment.Variables.Person[] where the Id value matches the nameId.

 SET name = the(select item fieldvalue(r.Name) from OutputLocalEnvironment.Variables.Person[] as r where r.Id = nameId);   


You may be interested in the following posts:

Good IIB ESQL Coding Practice

Properties versus MQMD folder behavior

Create a JSON Array in IBM Integration Bus

9 comments:

  1. Awesome notes thanks, How do i convert a JSON into a string?

    ReplyDelete
  2. I would do the following:-

    1. Convert JSON to BLOB
    2. Convert BLOB to char

    SET myBlob = ASBITSTREAM(InputRoot.JSON CCSID InputRoot.Properties.CodedCharSetId ENCODING InputRoot.Properties.Encoding);
    Set Value1 = CAST(myBlob as char CCSID InputRoot.Properties.CodedCharSetId ENCODING InputRoot.Properties.Encoding);

    ReplyDelete
  3. awesome , hope you keep adding

    ReplyDelete
  4. I want to iterate same XML messages using Cardinality function but I am able to fetch the last XML tag 3 times. pls help

    ReplyDelete
    Replies
    1. I would use a reference variable instead of using the CARDINALITY function.

      See this code snippet taken from IBM Knowledge Center (https://www.ibm.com/support/knowledgecenter/SSMKHH_10.0.0/com.ibm.etools.mft.doc/bj28653_.html).

      See this post why its better to use reference variables instead.
      https://nokstechnotes.blogspot.com/2019/03/good-iib-esql-coding-practice.html

      Delete
  5. Hi im receiving blob (contains token) from the response of a http request node. Tried your method #2 and #10 but no luck. Also, changed the properties of httprequestnode response parsing from blob to json but no luck. What is the alternative?

    ReplyDelete
  6. Are you getting an error? If yes, what is the error. Can you share your code.

    ReplyDelete
  7. any idea how to pass parameter to HTTP request? I tried outputroot not working {
    "account": "0005678",
    "OrderID": "12345",
    "OrdItms": [
    {
    "ItemNumber": "11111"
    },
    {
    "ItemNumber": "22222"
    }
    ]
    }

    ReplyDelete
    Replies
    1. See point 9 in the blog post, 9 Pass Parameters to an HTTP request Node.

      To pass the account number I would use the foolowing code.

      SET OutputLocalEnvironment.Destination.HTTP.QueryString.account =
      '0005678';

      Delete