App Connect

App Connect

Join this online user group to communicate across IBM product users and experts by sharing advice and best practices with peers and staying up to date regarding product enhancements.

 View Only
  • 1.  How to parse a JSON message in Java compute node

    Posted Wed February 14, 2024 05:08 PM

    In our flow we are setting the following variables and then trying to access these variables in Java compute node that is the next node...

    CREATE FILTER MODULE SchedulerInit_StoreRequest
    CREATE FUNCTION Main() RETURNS BOOLEAN
    BEGIN
    -- Define JSON domain in Global Environment
    CREATE LASTCHILD OF Environment.Variables DOMAIN('JSON') TYPE Name NAME 'schedulerMessage';
    -- Save the entire incoming request message
    SET Environment.Variables.schedulerMessage = Root.JSON.Data;
    -- continue normal processing
    RETURN TRUE;
    END;
    END MODULE;
    In the Java compute node
                    MbMessage globalenv = inAssembly.getGlobalEnvironment() ;
                    MbElement envVariables = globalenv.getRootElement().getFirstChild(); 
                    System.out.println("Element name is "+envVariables.getName()); -- prints "Element name is Variables"
    But when I try to get this "Environment.Variables.schedulerMessage"
                     System.out.println("Element is "+envVariables.getFirstChild());
    using the following it prints "Element is null" it should be schedulerMessage 
    I am not sure what am I missing here... it is quite annoying....
    Can some one please point out the issue here...
    Thanks in advance for the answer :-)


    ------------------------------
    Kareem Qureshi
    ------------------------------


  • 2.  RE: How to parse a JSON message in Java compute node

    Posted Thu February 15, 2024 03:15 AM

    Hi Kareem

    Is Root a reference you use, or should it be InputRoot?

    My output is this:

    2024-02-15 09:13:33.581    291 Element name is Variables
    2024-02-15 09:13:33.584    291 Element is MbElement( type: 1000000 name: schedulerMessage )

    In the esql node I have this code

    BEGIN
            CALL CopyMessageHeaders();
            --CALL CopyEntireMessage();
            CREATE LASTCHILD OF Environment.Variables DOMAIN('JSON') TYPE Name NAME 'schedulerMessage';
            SET Environment.Variables.schedulerMessage = InputRoot.JSON.Data;
            RETURN TRUE;
        END;

    And in the jcn it's this

        // Add user code below
                MbMessage globalenv = inAssembly.getGlobalEnvironment() ;
                MbElement envVariables = globalenv.getRootElement().getFirstChild();
                MbElement outRoot = outMessage.getRootElement();
                MbElement outJsonRoot = outRoot.createElementAsLastChild(MbJSON.PARSER_NAME);
                MbElement outJsonData = outJsonRoot.createElementAsLastChild(MbElement.TYPE_NAME, MbJSON.DATA_ELEMENT_NAME, null);
                MbElement outJsonBody = outJsonData.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "Message", "Hello World");
                //MbElement outJsonBody2 = outJsonData.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,"OriginalMessage", envVariables);
                //outJsonData.setValue(envVariables);
                System.out.println("Element name is "+envVariables.getName());
                System.out.println("Element is "+envVariables.getFirstChild());
                // End of user code

    The only difference I see here is the Root field you use.



    ------------------------------
    Regards
    Matthias Blomme
    ------------------------------



  • 3.  RE: How to parse a JSON message in Java compute node

    Posted Thu February 15, 2024 08:23 AM

    Instead of using 

     MbElement envVariables = globalenv.getRootElement().getFirstChild(); 

    Have you tried using: 

     MbElement envVariables = globalenv.getRootElement().getLastChild(); 

    And if you did what was the result?



    ------------------------------
    Francois Brandelik
    ------------------------------



  • 4.  RE: How to parse a JSON message in Java compute node

    Posted Fri February 16, 2024 09:42 AM

    Hi Francois,

     MbElement envVariables = globalenv.getRootElement().getLastChild(); 

    That works and we get Variables element but we need get this below element

                                    

    -- Define JSON domain in Global Environment
    CREATE LASTCHILD OF Environment.Variables DOMAIN('JSON') TYPE Name NAME 'schedulerMessage';
    -- Save the entire incoming request message
    SET Environment.Variables.schedulerMessage = Root.JSON.Data;

    globalenv.getRootElement().getLastChild().getLastChild() this is getting us "null"

    Thanks in advance



    ------------------------------
    Kareem Qureshi
    ------------------------------



  • 5.  RE: How to parse a JSON message in Java compute node

    Posted Mon August 26, 2024 08:23 PM

    Hello Kareem,

    You can try Eg:

    MbElement root = inMessage.getRootElement().getLastChild().getFirstElementByPath("/JSON/Data");



    ------------------------------
    Bikramjit Purkayastha
    ------------------------------



  • 6.  RE: How to parse a JSON message in Java compute node

    Posted Wed August 28, 2024 09:27 AM

    Hi Kareem,


    To provide the most accurate solution, it would be helpful to know which version of IBM App Connect you're using. There might be slight variations in accessing the JSON data depending on the version. In the meantime, you can check the documentation on accessing message data within the Java compute node.



    ------------------------------
    Warner Mark
    ------------------------------