Service Virtualization

Expand all | Collapse all

Working with JSR step after JSON Path Filter

  • 1.  Working with JSR step after JSON Path Filter

    Posted 02-14-2019 06:00 AM

    Will_Truong J_NeSmith Rick.Brown

     

    Hi Team,

    In a scenario I have extracted the below by using the filter:

    JSON Path : $[*].id

    JSON Value : {{ids}}

    JSON Length : {{len}}

     

    For a instance:

    ids = ["10","20","30"]

    len = 3

     

    After this I wanted to rephrase the final result as below in a string exactly as written below:

     

    FinalString = testExec.setStateValue({"id="{{ids[1]}}",This is first" ; "id="{{ids[2]}}",This is second" ; ....});

     

    Can some one please help me with the JSR code i should use for the above scenario.

     

    Thanks and Regards,

    Bibhu



  • 2.  Re: Working with JSR step after JSON Path Filter

    Posted 02-14-2019 07:50 AM

    Hi Bibhu --

     

    I don't understand what your output is supposed to be, I'm afraid. It looks like a piece of pseudo-JSON with different syntax. Is this a standardised format, and is a JAR available for it, so a constructor class for it can be read into a scripting step? From the example above, and with a little squinting, it looks like a required output might be something like:

       { "id=10,This is first" : "id=20,This is second" : "id=30,This is third" : "id=40,This is fourth" }

    What are you going to do with this output? There might be a common standard that can read the data, which we could be using instead.

     

    I also don't understand where the strings "first", "second", etc come from. Do you want to create an array mapping of

    elementNumber,elementString

       0,"first",

       1,"second",

       2,"third",

       3,"fourth",

       4,"fifth",

       etc?

     

    Rick



  • 3.  Re: Working with JSR step after JSON Path Filter

    Posted 02-14-2019 08:34 AM

    Thanks Rick.Brown, for the prompt reply!!

     

    Let me put it step by step.

     

    1. We have a HTTP listener 1504
    2. Have filtered out id and name by using JSON Path Filter

       

    JSON Path : $[*].id

    JSON Value : {{ids}}      ~ This filter is filtering out (ids=["001","002"])

    JSON Length : {{len}}    ~ This filter is filtering out (len=2)

     

    JSON Path : $[*].name

    JSON Value : {{names}}         ~ This filter is filtering out (names=["abc","xyz"])

    JSON Length : {{nameleng}}   ~ This filter is filtering out (nameleng=2)

     

    3. Performing a JSR step in which i can rephrase the filtered values and create a complete response. planning to store the complete response in a property for further use.

     

    4. Will send back the response back.

     

    Sample Format of Incoming Request:

    [{"id"="001","name"="abc"},{"id"="002","name"="xyz"}]

     

    Expected Response: (stored in a property):

    {"Checklist":[{"id"="001","name"="abc","comments"="This is same"},{"id"="002","name"="xyz","comments"="This is same"}]}

     

    Intention of code to perform in JSR is to store the above Expected Response in a property, e.x. FinalResponse = {"Checklist":[{"id"="001","name"="abc","comments"="This is same"},{"id"="002","name"="xyz","comments"="This is same"}]}

     

    Kindly let me know in case any more details are required.

     

    Regards,

    Bibhu



  • 4.  Re: Working with JSR step after JSON Path Filter

    Posted 02-14-2019 08:58 AM

    Hi Bibhu --

     

    I suppose the bit I'm failing to understand is your message format. The sample format of the request that you have in step 4 isn't JSON, and I don't understand how the JSON filter is able to extract usable data from it. Your expected response on the same step isn't JSON either.

     

    Any assertion you while using this custom format will only be able to return a True or False on the entire string, because DevTest won't understand the message format you're using.

     

    Alternatively, it might be that your messages are, in fact, JSON, and something has gone wrong in displaying it in this discussion. Do you have complete sample messages (with masked data) that can be attached in a ZIP file here, so we can determine if this is the case?

     

    By the way, because you're doing validation, is this using DevTest in testing mode, rather than Service Virtualization mode?

     

    Rick