App Connect

 View Only
  • 1.  How to convert json string and blob to json object

    Posted Thu May 30, 2024 03:38 AM
    Edited by Elshaday Teshome Thu May 30, 2024 03:38 AM

    I am new to app connect and esql. I want to convert a string json and blob to json object. I tried to use the create and parse function like this:

    SET jsonString = InputRoot.JSON.Data;

    CREATE LASTCHILD OF OutputRoot DOMAIN('JSON') PARSE(jsonString);

    I tried other ways as well but I keep getting errors one of them being BIP2907. Is there any other way to do it?



    ------------------------------
    Elshaday Teshome
    ------------------------------



  • 2.  RE: How to convert json string and blob to json object

    IBM TechXchange Speaker
    Posted Thu May 30, 2024 07:32 AM

    Hi Elshaday,

    To guide you correctly, it would be nice to have a bit more context about the problem you are trying to solve in the message flow ... What kind of message domain are you using to parse the inbound data (before it hits the Compute node ESQL)? It looks like you have a JSON domain message coming in ... because your first line of ESQL refers to "InputRoot.JSON.Data" ... but if this is the xase, then why are you trying to do further parsing in the JSON domain given your message is already in the JSON domain? I'm confused :) 

    Trying to answer you in a generic way, if you imagine you had a flow HTTPInput > Compute > HTTPReply ... and on the HTTPInput node you used the BLOB message domain (which will be used by default when you placed the HTTPInput node in to the flow).... In this circumstance if the inbound data was valid JSON (but parsed as a BLOB input) then you could use a Compute node ESQL to switch the message to a parsed JSON representation in the JSON domain using code like this:

        BEGIN
            CALL CopyMessageHeaders();
            CREATE LASTCHILD OF OutputRoot DOMAIN('JSON') PARSE(InputRoot.BLOB.BLOB);
            RETURN TRUE;
        END;

    You'll note that the parameter I'm passing in to the PARSE clause has a BLOB data type. This code will write out the same JSON to the output ...

    Perhaps more useful as an illustrative example would be a flow which uses a string ... So in the snippet below I declare and cast a string to binary data and then invoke the CREATE statement ....

        BEGIN
            CALL CopyMessageHeaders();
            DECLARE myJSONString CHAR '{"Hello":"World"}';
            CREATE LASTCHILD OF OutputRoot DOMAIN('JSON') PARSE(CAST(myJSONString AS BLOB CCSID 1208));
            RETURN TRUE;
        END;

    Hope this helps ... if I've misunderstood please describe more about the flow context ... e.g. what does the input and output data look like .... and perhaps a flow picture would help us understand.

    Cheers,

    Ben



    ------------------------------
    Ben Thompson
    IBM UK
    ------------------------------



  • 3.  RE: How to convert json string and blob to json object

    Posted Thu May 30, 2024 08:46 AM
    Edited by Elshaday Teshome Thu May 30, 2024 08:48 AM

    I guess it is better if I explain the message flow. I have a main flow I built in the App Connect toolkit. In the main flow, I'm invoking a callable flow which I built using the designer in APP connect Saas. The callable flow is used to communicate to on on-premise API using the secure agent. In the callable flow, I added the callable flow input node, HTTP invoker node, and callable flow reply node. In the reply node, I set Message.Json.Data as HTTP Response body which shows a warning stating that "results in a string, not an object." In the main flow, I add a compute node to manipulate the incoming data. When I added :                   

    SET OutputRoot.JSON.Data = InputRoot.JSON.Data 

    It is throwing an error stating that "element must be of type JSON.Array or JSON.Object." In the user trace log it is showing the InputRoot.JSON.Data is resolving to a string JSON something like this

    '{"hello": "world}'.

    I want to convert it into a JSON payload but I am confused about how to do it.



    ------------------------------
    Elshaday Teshome
    ------------------------------



  • 4.  RE: How to convert json string and blob to json object

    IBM TechXchange Speaker
    Posted Thu May 30, 2024 10:12 AM

    Thank you for supplying some wider context to your problem. For future postings, it can be very helpful to provide a quick screenshot of the flow and nodes so that there can be no confusion ... especially if there is more than one flow involved. Piecing together your description it sounds like you have flows like this:

    Toolkit message flow:    HTTPInput > CallableFlowInvoke > Compute > HTTPReply

    Designer message flow:  CallableInput > HTTPInvoke > CallableReply

    It wasn't clear if the Compute node in the Toolkit message flow was before or after the CallableFlowInvoke node. It sounded like your Compute node is after the CallableFlowInvoke node and its only purpose is intended to transform the response message which comes back from the Designer message flow CallableReply.

    I tried following your description of how you have configured CallableReply in the Designer flow. I include my own screenshot below. 

    In my example I have chosen to have a single string property called "Ben1" and I have assigned the name of the Designer flow to this property. This means that when the data is passed back to the Toolkit flow, it will already be in the JSON Domain and the tree structure will look like this:

    JSON

      |-- Data 

               |-- Ben1 = "BensMessageFlowName"

    As I understand it, the value of the string property in your example is taken from the HTTP reply body which is returned to the HTTPInvoke node prior to the CallableReply. It sounds like what you are wanting to do is to parse this response body (perhaps using the JSON Parse node?) so that you end up with string values which can be passed back to the Toolkit flow? What is the body which is returned to the HTTPInvoke ... is it always JSON, or could it be a string field? I noticed in your example you mention there appeared to be no closing quote after "world" ... is it possible that the value being returned to the HTTPInvoke is not proper JSON? If it is guaranteed to always be JSON then I think you should be able to return that JSON structure to the Toolkit flow without first "stringifying" it. 

    Either way, to give you a literal answer to your direct question ... it looks like your Compute node ESQL is not attempting to convert a string into a JSON message ... currently your code is "SET OutputRoot.JSON.Data = InputRoot.JSON.Data" ... This will just copy the JSON.Data tree from input to ouput.

    My code sample from my last reply should help here ... instead of using the String I declared you could take the string input from the message which comes back from the CallableReply ... for example if my field "Ben1" carried a true well formed piece of JSON (as opposed to the name of the message flow which would just be a string) then the following code would work ....

        BEGIN
            CALL CopyMessageHeaders();
            DECLARE myJSONString CHAR;

            SET myJSONString = InputRoot.JSON.Data.Ben1;
            CREATE LASTCHILD OF OutputRoot DOMAIN('JSON') PARSE(CAST(myJSONString AS BLOB CCSID 1208));
            RETURN TRUE;
        END;



    ------------------------------
    Ben Thompson
    IBM UK
    ------------------------------



  • 5.  RE: How to convert json string and blob to json object

    Posted Thu May 30, 2024 10:28 AM

    Hi Elshaday,

    Ben's message provides a great explanation of JSON parsing in ESQL, but it seems you might already have a parsed JSON message.

    The examples show converting BLOB data or strings into parsed JSON. If your scenario differs, sharing more context about the message flow and data format (maybe a diagram?) would help clarify things.

    Cheers!



    ------------------------------
    Frelan Hardware
    ------------------------------