3
votes

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.

2
Use $..cohortDefinition.srcId in the JSON Path Extractor. myVar_1 will have the first srcId.NaveenKumar Namachivayam
I tried that option. This results in 3526 and 3525Sachetan
Yes. You need to use myVar_1 in your test plan which will retrieve first srcId.NaveenKumar Namachivayam
This worked. Thanks a lot NaveenSachetan

2 Answers

2
votes

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}

0
votes

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