Search code examples
jsonjmeterextract

Extract only the first id from json (JMeter - JSON Extractor)


I have a problem to extract only the first id from json using "JSON Extractor" in JMeter and will be thankful if someone help me.

Example json:

{
 "content" : [
   {
      "id": 1,
      "name":"John",
      "data" : {
          "id" : 101,
          "dataType" : "exampledata1" }
   },
   {
       "id": 2,
       "name":"Johnatan",
       "data" : {
           "id": 102,
           "dataType": "exampledata2"}
   }
}

My goal is to extract only first id values (1,2 .. etc).

With JSON Extractor - expression $..id and match no. -1, it extracts all the values, like:

id_1=1
id_2=101
id_3=2
id_4=102

I want:

id_1=1
id_2=2

Thank you in advance!


Solution

  • .. is a "deep scan" operator in JsonPath, it will return values of all id attributes wherever they are.

    You need to limit your lookup to direct children of content node only, i.e. use the following expression:

    $.content.*.id 
    

    enter image description here

    More information: How to Use the JSON Extractor For Testing