0

Sample JSON body from an http request,

[
    {
        "date-range": {
            "high": "2020-09-15",
            "low": "2020-09-13T18:30"
        },
        "visit-identifiers": {
            "system-id": 4,
            "compound-id": "1196_AFG_2_4"
        },
        "study-site": {
            "study": {
                "system-id": 1196,
                "study-protocol-version": "TEST TRIAL 01"
            },
            "timezone": {
                "code": "Asia\/Calcutta",
                "display-name": "(GMT+5:30) Bombay, Calcutta, Madras, Mumbai, New Delhi"
            },
            "organization": {
                "system-id": 8,
                "primary-centre-short-name": "(RNSH) - Kolling Institute of"
            },
            "study-country": {
                "country-code": "AFG",
                "display-name": "Afganasthan"
            },
            "study-site-identifiers": {
                "system-id": 2,
                "long-display-id": "Mr Perf_HYD_ClinPersonnel Test",
                "site-reference": "2",
                "short-display-id": 2
            }
        },
        "defined-activity": {
            "display-name": "NOV-Monitoring",
            "visit-type": "MON"
        },
        "primary-investigator": {
            "title-prefix": "Mr.",
            "system-id": 27,
            "display-name": "Mr. Clinical Mr Perf_HYD_ClinPersonnel Test",
            "voice-telecom-address": "Duplicate 1",
            "title-suffix": "Test",
            "email-telecom-address": "[email protected]",
            "given-name": "Clinical ",
            "middle-name": "",
            "family-name": "Mr Perf_HYD_ClinPersonnel"
        },
        "status": {
            "code": "ON",
            "display-name": "Ongoing"
        }
    },
    {
        "date-range": {
            "high": "2020-06-05",
            "low": "2020-06-05"
        },
        "visit-identifiers": {
            "system-id": 5,
            "compound-id": "1196_AFG_2_5"
        },
        "study-site": {
            "study": {
                "system-id": 1196,
                "study-protocol-version": "TEST TRIAL 01"
            },
            "organization": {
                "system-id": 8,
                "primary-centre-short-name": "(RNSH) - Kolling Institute of"
            },
            "study-country": {
                "country-code": "AFG",
                "display-name": "Afganasthan"
            },
            "study-site-identifiers": {
                "system-id": 2,
                "long-display-id": "DISP_ID_1",
                "site-reference": "2",
                "short-display-id": 2
            }
        },
        "defined-activity": {
            "display-name": "NOV-Monitoring",
            "visit-type": "MON"
        },
        "primary-investigator": {
            "title-prefix": "Mr.",
            "system-id": 27,
            "display-name": "TEST NAME",
            "voice-telecom-address": "Duplicate 1",
            "title-suffix": "Test",
            "email-telecom-address": "[email protected]",
            "given-name": "Clinical ",
            "middle-name": "Test",
            "family-name": "Mr Perf_HYD_ClinPersonnel"
        },
        "status": {
            "code": "PL",
            "display-name": "Planned"
        }
    },
    {
        "date-range": {
            "high": "2020-06-06",
            "low": "2020-06-06"
        },
        "visit-identifiers": {
            "system-id": 6,
            "compound-id": "1196_AFG_2_6"
        },
        "study-site": {
            "study": {
                "system-id": 1196,
                "study-protocol-version": "TEST TRIAL 01"
            },
            "timezone": {
                "code": "Pacific\/Midway",
                "display-name": "(GMT-11:00) Midway Is, Samao"
            },
            "organization": {
                "system-id": 8,
                "primary-centre-short-name": "(RNSH) - Kolling Institute of"
            },
            "study-country": {
                "country-code": "AFG",
                "display-name": "Afganasthan"
            },
           "study-site-identifiers": {
                "system-id": 2,
                "long-display-id": DISP_ID_1",
                "site-reference": "2",
                "short-display-id": 2
            }
        },
        "defined-activity": {
            "display-name": "NOV-Contact Visit",
            "visit-type": "CTT"
        },
        "primary-investigator": {
            "title-prefix": "Mr.",
            "system-id": 27,
            "display-name": "TEST NAME",
            "voice-telecom-address": "Duplicate 1",
            "title-suffix": "Test",
            "email-telecom-address": "[email protected]",
            "given-name": "Clinical ",
            "middle-name": "Test",
            "family-name": "Mr Perf_HYD_ClinPersonnel"
        },
        "status": {
            "code": "PL",
            "display-name": "Planned"
        }
    }
]

from the above JSON response data I want to fetch the compound ID which has status code "PL". In the above JSON have 3 compound id,

"compound-id": "1196_AFG_2_4" -- "code": "ON"
"compound-id": "1196_AFG_2_5" -- "code": "PL"
"compound-id": "1196_AFG_2_6" -- "code": "PL"

Want to select any one from the above which has status code PL. Currently I am using

Regex: "compound-id":"(.+?)"
Template: $1$
match No: 0

Is there any way we can put a condition in regex?

2 Answers 2

1

Using regular expressions for parsing JSON is not the best idea, JMeter comes with JSON Extractor which allows executing arbitrary JsonPath queries which are more stable, easier to read/maintain and they enable some complex use cases which cannot be implemented with regular expressions.

For particular your use case the relevant expression would be something like:

$..[?(@.status.code == 'PL')].visit-identifiers.compound-id

Demo:

enter image description here

More information: API Testing With JMeter and the JSON Extractor

Sign up to request clarification or add additional context in comments.

2 Comments

if I have restriction to use JSO extractor as it is deprecated
I'm not aware of any deprecation facts or plans for the JSON Extractor, it appeared in JMeter 3.0 and it's present in the latest JMeter 5.3 without any signs of being deprecated
0

You are missing a space in regex

"compound-id": "(.+?)"

And if you set Match No. as -1 you will get all the values (a is the variable name):

a_1=1196_AFG_2_4
a_2=1196_AFG_2_5
a_3=1196_AFG_2_6

for example when the input variable has the name inputVar, the following variables should have been defined:

inputVar_1 = wendy inputVar_2 = charles inputVar_3 = peter inputVar_4 = john

You can also use (\w+) instead of space, but then use template $2$ to get the value

Also you can use JSON Extractor to get values as @DimtriT suggested

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.