Service Virtualization

 View Only
Expand all | Collapse all

Is there a filter or sample code to extract 'n' number of occurrences of particular argument in json response

  • 1.  Is there a filter or sample code to extract 'n' number of occurrences of particular argument in json response

    Posted Jul 03, 2019 03:10 PM
    Hi,

          I have a JSON response where a key-value pair is present in multiple places in the JSON response. Is there a way to extract all the occurrences of that particular key-value pair. Example below

    basicInfo":{  
             "model":null,
             "operatingSystem":null,
             "salePrice":"849.99",

             "manufacturer":null
          },
          "price":{  
             "usageContext":null,
             "salePrice":"1149.99",
             "selectable":"true",
             "listPrice":"1149.99"
          },


    The requirement is  to take these 2 occurences of  "salePrice", and show it in OutputLogMessage step. 

    So my OutputLogMessage should look like 

    "salePrice":"849.99",
    "salePrice":"1149.99",

    Could someone specify if there is a way to extract this.


  • 2.  RE: Is there a filter or sample code to extract 'n' number of occurrences of particular argument in json response
    Best Answer

    Posted Jul 04, 2019 01:51 AM
    Hi Varun,

    You can use JSON Xpath to retrive these values.

    Option1:
    use two variables with different Xpaths (as your structure is diff for both blocks):
    $.basicInfo.salePrice
    $.price.salePrice
    and use the variables in Output log message

    Option 2:
    Use JSONPath query $.*.['salePrice'] to get list of all the sales price in JSON.