1

I have this json:

{
    "-1":     {
        "description": "test1"
    },
    "222":     {
        "description": "test2"
    },
    "223":     {
        "description": "test3"
    },
    "224":     {
        "description": "test4"
    },
    "recordsCount": 4
}

Using this Json path expression: $.. it returns all the Json. I would like to find the Json path expression that returns only the ID values: "-1" "222" "223" "224"

Can you help me?

wp78de
  • 18,207
  • 7
  • 43
  • 71
sarjaana
  • 77
  • 1
  • 1
  • 6

1 Answers1

0

Accessing the property (key) instead of the value is a feature not present in the original JSONPath specifications, and only a few implementation support this feature. So, no luck using the JMeter JsonPath Plugin/Extractor extracting only property names.
For instance, JsonPath-Plus for JavaScript offers the operator ~ for grabbing property names of matching items.

As an alternative, you could use JSR223 Groovy to get the results from JSON:

import groovy.json.JsonSlurper;
import groovy.json.JsonOutput; //if output as JSON is needed

JsonSlurper JSON = new JsonSlurper ();
jsonResponse = JSON.parseText(prev.getResponseDataAsString());
//println json.keySet() //[-1, 222, 223, 224, recordsCount]
vars.put("firstName", response.members[2].firstName.toString());
Set<String> keySet=obj.keySet();
List<String> keys = [];
// iterate over the key set, adding only numbers to result list
for(String key:keySet){
    if(key.isNumber())
        keys.add(key);
}
//println(keys); //[-1, 222, 223, 224]
def output = JsonOutput.toJson(keys); //convert to JSON string if neede
//println(output); //["-1","222","223","224"]
vars.put("jsonKeys", output.toString());

Demo: Online Groovy sample.

More information:

wp78de
  • 18,207
  • 7
  • 43
  • 71