Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

jmeter - Select random value from the response json with a validation

When I send a request to an api end point, the following json is coming in my response.

{
    "entities": {
        "practitioners": {
            "f1d26a4b-c489-493d-bccf-7b9c8b92ecac": {
                "fullAvatarUrl": null,
                "id": "f1d26a4b-c489-493d-bccf-7b9c8b92ecac",
                "accountId": "ef757dba-f0d5-4464-a338-4a810e02bf47",
                "pmsId": "1",
                "type": "Dentist",
                "isActive": true,
                "isHidden": false
            },
            "ee87642d-c9a6-4a9d-b99a-a96501f27a7b": {
                "fullAvatarUrl": null,
                "id": "ee87642d-c9a6-4a9d-b99a-a96501f27a7b",
                "accountId": "ef757dba-f0d5-4464-a338-4a810e02bf47",
                "pmsId": "2",
                "type": "Hygienist",
                "isActive": true,
                "isHidden": false
            },
            "d0aeb9eb-f267-45ad-8cdf-eada1155c274": {
                "fullAvatarUrl": null,
                "id": "d0aeb9eb-f267-45ad-8cdf-eada1155c274",
                "accountId": "ef757dba-f0d5-4464-a338-4a810e02bf47",
                "pmsId": "3",
                "type": "Dentist",
                "isActive": true,
                "isHidden": false
            },
            "2f641e8e-c5d6-4fdf-8fbe-f99fe837f441": {
                "fullAvatarUrl": null,
                "id": "2f641e8e-c5d6-4fdf-8fbe-f99fe837f441",
                "accountId": "ef757dba-f0d5-4464-a338-4a810e02bf47",
                "pmsId": "4",
                "type": "Hygienist",
                "isActive": true,
                "isHidden": false
            }
        }
    },
    "result": [
        "f1d26a4b-c489-493d-bccf-7b9c8b92ecac",
        "ee87642d-c9a6-4a9d-b99a-a96501f27a7b",
        "d0aeb9eb-f267-45ad-8cdf-eada1155c274",
        "2f641e8e-c5d6-4fdf-8fbe-f99fe837f441"
    ]
}

problem statement: I want to select any one from the above 4 random value(want to select the practitioners id and which are sitting at the first element under practitioners) with a validation that isActive should be true and isHidden should be false.

I have tried using the JSON extractor using the expression $.entities.practitioners and match number 0

But it is not selecting the any one rather it select all.

question from:https://stackoverflow.com/questions/65871476/select-random-value-from-the-response-json-with-a-validation

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I guess this approach can help you,

add a JSR223 PostProcessor to your http request and after that please add the following groovy script to the PostProcessor

Note: this is only to select the random PractitionersId for the validation just use the similar logic.

import groovy.json.*

def response = prev.responseDataAsString ;
def json = new JsonSlurper().parseText(response) ;
def sizeResultPractitioners = json.result.size();
Random rnd = new Random()
def randomResultPractitioners = rnd.nextInt(sizeResultPractitioners);
log.info("---------->"+randomResultPractitioners);
log.info("---------->"+json.result[randomResultPractitioners]);

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...