4

I am trying to extract first element from a json array. Below mentioned is json array

   [
    {
        "cohortDefinition": {
            "Key": 1151,
            "id": 1798,
            "srcId": "3526",
            "pcKey": -1,
            "userName": "CHROME_USER",
            "name": "JMeter2017-01-06-1483749546167",
            "Type": "SUBJECT",
            "tool": "CB",
            "count": 32757,
            "extractionStatus": "",
            "dateCreated": "2017-05-10T17:48:45Z"
        },
        "datasource": {
            "id": 2,
            "name": "health",
            "subjectCount": 116352
        },
        "project": {
            "id": 747,
            "name": "Jmeter Project"
        }
    },
    {
        "cohortDefinition": {
            "Key": 1150,
            "id": 1796,
            "srcId": "3525",
            "pcKey": -1,
            "userName": "CHROME_USER",
            "name": "JMeter2016-10-27-1477620919644",
            "Type": "SUBJECT",
            "tool": "CB",
            "count": 32757,
            "extractionStatus": "",
            "dateCreated": "2017-05-10T16:57:11Z"
        },
        "datasource": {
            "id": 2,
            "name": "health",
            "subjectCount": 116352
        },
        "project": {
            "id": 747,
            "name": "Jmeter Project"
        }
    }
]

From above json i would like to extract first value ie. srcId": "3526". I tried doing following expression in Jmeter extractor

$..cohortDefinition.srcId[1]

However it is not working. If anyone know how to do this please do let me know.

6
  • Use $..cohortDefinition.srcId in the JSON Path Extractor. myVar_1 will have the first srcId. Commented May 10, 2017 at 22:59
  • I tried that option. This results in 3526 and 3525 Commented May 10, 2017 at 23:53
  • Yes. You need to use myVar_1 in your test plan which will retrieve first srcId. Commented May 10, 2017 at 23:55
  • Please check this image imgur.com/a/rzA8p and imgur.com/a/8rFfd Commented May 10, 2017 at 23:56
  • This worked. Thanks a lot Naveen Commented May 11, 2017 at 3:22

2 Answers 2

3

After JMeter 3.0, you can use JSON Extractor, see:

Before JMeter 3.0:

Please follow the below steps to retrieve srcId.

  1. Add a JSON Path Extractor to your request and configure below values.

Destination Variable Name - myVar

JSON Path Expression - $..cohortDefinition.srcId - this will extract all the srcIDs from the JSON.

Default Value - Not Found or Err

JSON Path Config

  1. Add a Debug Sampler and View Results Tree to your test plan.

  2. Save it and execute.

  3. In Debug Sampler, you can view all the srcId as shown below.

Output

You can now use myVar_1 and myVar_2 in your test plan using ${myVar_1} ${myVar_2}

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

Comments

1

No need for Plugin, JMeter has a JSON Extractor that will provide this feature:

JSON Extractor configuration

Notice:

JSON Path Expression is: $..cohortDefinition.srcId

Match No : 1

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.