Service Virtualization

Expand all | Collapse all

Reading all entries of an array from JSON request

  • 1.  Reading all entries of an array from JSON request

    Posted Nov 09, 2017 03:37 PM

    Hi everyone,

    I am trying to build a virtual service for a REST API. The request is a JSON which has the first name, last name and an array of payments. The response is a JSON message which has the first name, last name, total amount of all payments from the request, and the array of payments (from the request) with a new tag element for each payment entry. I have attached a sample request and response. 

    I am trying to build a dynamic response for the meta that can handle any number of payments from the request properly (i.e. shows are the payment entries from the request with the added tag and the total amount). I have tried the scriptable data protocol to update the response but it doesn't work.

    I was wondering if someone can give me a hint on how to build that dynamic response.

    Thanks for your help.

    Attachment(s)

    zip
    Payment-1-REQ.txt.zip   292 B 1 version
    zip
    Payment-1-RSP.txt.zip   318 B 1 version


  • 2.  Re: Reading all entries of an array from JSON request
    Best Answer

    Posted Nov 13, 2017 02:32 PM
      |   view attached

    Take care that solving for one set of requirements does not lead to the implementation of new requirements. For example, the next requirement might be that the "total amount" element in the JSON response must equal the sum of the actual "amounts" elements on the incoming request. This requirement will lead to additional logic to determine the value (i.e., yes or no) for the "processed" element in the JSON response. The resulting increase in requirements will slowly increase the amount of "business" logic that needs to be duplicated in the service. This is something you will want to guard against.

     

    Having said that, the attached is a hack demonstrating how to replace the response with a variable number of occurrences. The Scriptable adds "processed":"yes" to each element in the JSON array. The META on the VSI replaces the static JSON with the property created by the Scriptable. Only answers matching the exact signature of 3 array elements falls into the specific response. 

    Attachment(s)

    zip
    DynamicJSONArray.zip   17 KB 1 version


  • 3.  Re: Reading all entries of an array from JSON request

    Posted Nov 15, 2017 01:42 PM

    Thanks Joel for your reply and snippet. If the request and response were XMLs, what could I use to replace the JSON Path Finder filter?



  • 4.  Re: Reading all entries of an array from JSON request

    Posted Nov 15, 2017 01:57 PM

    You could replace with the JSON Path filter with an XML XPath filter and accomplish similar results. The Generic XML DPH probably will not do what you are seeking.



  • 5.  Re: Reading all entries of an array from JSON request

    Posted Nov 15, 2017 04:28 PM

    Thanks Joel. On the XML input, I am using XML Path filter and it works if the number of payments stays the same. As soon as I add a new payment in the request, the virtual service returns 404. 



  • 6.  Re: Reading all entries of an array from JSON request

    Posted Nov 15, 2017 04:50 PM

    Sorry, I do not have a copy of the virtual service or the XPath and any script logic you might be using. Couple things you might try...  

    1) Ensure the XPath obtains all of the occurring data and not just one occurrence. Validate any script logic as well.

    2) Assuming the LISTEN step is working, check the META response Match Style in the VSI. If META is set to match on Signature, change this to match on Operation.  Leave the specific transaction as an Exact match. I am assuming the {{ }} replacement for the variable number of occurring data is happening on the META transaction.



  • 7.  Re: Reading all entries of an array from JSON request

    Posted Nov 16, 2017 05:41 PM

    Thanks Joel for your help. I had forgotten to set META to Operation. It is working fine now.



  • 8.  RE: Re: Reading all entries of an array from JSON request

    Posted Jul 30, 2020 05:41 PM
    I am having same situation and when I changed meta match style to operation and handled array of payments...but now it's matching with one response in meta not with the other one ...earlier responses were matched based on argument value like offset=0 for first response and offset=25 for second response...now I am not having idea how can I configure these responses and handle dynamic array at the same time


  • 9.  RE: Re: Reading all entries of an array from JSON request

    Posted Jul 31, 2020 08:55 AM
    Hi Sourabh,

    Is it possible for you to share a sample message and elaborate on the requirement as well. 

    Make sure there is no match script at transaction level disabling the exact match criterias.

    Thanks.

    ------------------------------
    Regards,
    Vaibhav Jain
    Capgemini
    ------------------------------



  • 10.  RE: Re: Reading all entries of an array from JSON request

    Posted Aug 06, 2020 10:53 AM
    I have used operations match style and it worked ...thanks for your support 


  • 11.  Re: Reading all entries of an array from JSON request

    Posted Dec 04, 2017 04:43 AM

    Hi Joel,

     

    Im getting the below error while trying to open the VSM. Kindly help.

     

    Message: Missing VSE data protocol handler class.
    ----------------------------------------------------------------------------
    | Trapped Exception: com.itko.lisa.vse.stateful.protocol.rest.RestDataProtocol
    | Trapped Message: java.lang.ClassNotFoundException: com.itko.lisa.vse.stateful.protocol.rest.RestDataProtocol



  • 12.  Re: Reading all entries of an array from JSON request

    Posted Dec 04, 2017 08:38 AM

    What version of DevTest are you using? This VSM was built using version 10.1.