Forum Discussion

Rudradip's avatar
Rudradip
New Contributor II
7 years ago

How to put string concatenation inside the JSON Generator snap body?

Hi Guys ,

My scenario is following : I need to pass the whole Json inside the JSON generator snap .I have checked the velocity documnets that concate works by just appending ,like $a$b will concate the content of $a and $b . But in this case it is not working .

Json_input.zip (383 Bytes)

Here the “_” are for pipeline parameters and $ is for the variables coming from previous mapper .

Please help regarding this .

5 Replies

  • gowdhaman008's avatar
    gowdhaman008
    New Contributor II

    Hi,

    To read the value from incoming mapper you just need to use $ and read from parameter you need to use ${_parameterName}.

    [
        {
            "readFromUpStreamMapper": $a,
            "readFromPrameter" : ${_parameterB}
        }
    ]
    
    • Rudradip's avatar
      Rudradip
      New Contributor II

      yes @gowdhaman008 I know these things .I have tried in that way also . Same error is coming .

      I have tried with this :

      "{

      "remove": {
        "index": $CURRENT_IDX,
        "alias": ${_IDX_ALIAS}$Suffix_batch
        }
      

      }"

      My main issues is for the concatenate part . That is giving the trouble .

      The following is working just fine 🙂

      "{

      "remove": {
        "index": $CURRENT_IDX,
        "alias": ${_IDX_ALIAS}
        }
      

      }"

      • robin's avatar
        robin
        Former Employee

        @Rudradip This is a follow up after a considerable period of time but I wanted to answer the question.

        Assuming in input document to the JSON Generator like:

        {
          "Body": "456",
        }
        

        This concatenation in the JSON Generator WILL NOT work:

        [
            {
                "Response": {
                    "Message": "Thank you for your message, which was $Body"
                }
            }
        ]
        

        and will result in an error like

        Failed to validate the output
        
        Resolution:
        Verify that the table values and the template are correct
        
        Reason:
        Unexpected character ('4' (code 52)): was expecting comma to separate Object entries
        

        But if you use a Velocity Directive, the following WILL work:

        #set( $message = "Thank you for your message, which was $Body" )
        [
            {
                "Response": {
                    "Message": $message
                }
            }
        ]
        

        The output document will look like:

        [{
        	"Response": {
        		"Message": "Thank you for your message, which was \"456\""
        	}
        }]
        

        Do note that the $Body value has been wrapped in quotes. I’ll investigate if there is a way to remove those.

  • @Rudradip, I think the issue is concatenating a pipeline variable and a regular variable which causes the syntax error. If you concatenate two regular variables it works fine. For example: $CURRENT_IDX$SUFFIX_BATCH

    If you concatenate two pipeline variables, it will cause the syntax error:
    $_IDX_ALIAS$_IDX_ALIAS

    or even with a different syntax: ${_IDX_ALIAS}${_IDX_ALIAS} will cause the syntax error

    I am copying @tstack and @cjhoward18 to comment on this as they may be able to share better insights into this.