简体   繁体   中英

soap ui retrieving value from json response using groovy script

how can i get the sample id of a product for which status is 'available' from the json response using groovy script
[ { "sampleId": "17", "partNumber": "83729219", "sampleName": "Peter", "shortDescription": "Peter", "description": "Peter", "productUrl": " https://www.alwaysdis ", "sampleImage": " http://sit1.author.gillette ", "sku": null, "categoryId": 4, "parentSampleId": null, "locale": "en-GB", "variantName": "Pads Plus", "stockCount": 0, "noOfOrderPerCategory": 0, "noOfOrderPerSample": 0, "status": "NOT_AVAILABLE", "variants": [] }, { "sampleId": "27", "partNumber": "83729568", "sampleName": "ALWAYS DISCREET Incontinence Pants Normal", "shortDescription": null, "description": "\\n

  • ALWAYS DISCREET Incontinence Pants for sensitive bladder. RapidDry an", "productUrl": " https://www.alwainence-pants/always-discreet-medium-incontinence-pants ", "sampleImage": "/sitecore/media library/AlwaysDiscreet_UK/Im", "sku": null, "categoryId": 4, "parentSampleId": null, "locale": "en-GB", "variantName": " ", "stockCount": 79, "noOfOrderPerCategory": 0, "noOfOrderPerSample": 0, "status": "AVAILABLE", "variants": [] } ]

    how can i get the sample id of a product for which status is 'available' from the json response using groovy script.

  • You can use JSONSlurper to parse the String and then just use a Closure to filter the data, like this:

    def slurper = new groovy.json.JsonSlurper()
    def result = slurper.parseText(jsonData)
    def availableSamples = result.findAll{it.status == 'AVAILABLE'}
    

    The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM