Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jmeter : How to extract first element from json array

Tags:

jmeter

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.

like image 701
Sachetan Avatar asked Sep 06 '25 03:09

Sachetan


1 Answers

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

  • https://stackoverflow.com/a/47043204/460802

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}

like image 166
NaveenKumar Namachivayam Avatar answered Sep 07 '25 22:09

NaveenKumar Namachivayam