Kac
Kac

Reputation: 194

how can I get more than one element from json using jsonpath?

I have a simple JSON and I would like to get two properties :

core and matching and verify if core : true and matching : true.

   {

  "lockVersion" 
    : 1,

    "updatedBy" : "jan",

    "updatedOn" : "2016-09-25T11:21:45Z",

    "id" : 964,

    "title" : "Corporate Numeric",

    "description" : null,

    "descType" : 31084140,

    "descValueType" : 31084136,

    "defaultSourceOfVerification" : "Source",

    "core" : true,

    "matching" : true,

    "anything" : 
    [

    ],

  "authorized" 
    : 
    [
         1 
    ]


}

Is it possible to do this using AND operator or must I perform a two step action to extract one set then filter again to get my final results? I'm going to use jp@gc - JSON Path Assertion.

Upvotes: 2

Views: 1406

Answers (3)

UBIK LOAD PACK
UBIK LOAD PACK

Reputation: 34566

See Dmitri T answer for JSON Path Assertion plugin.

So Using Core JMeter you can do it like this using JSON Extractor and Response Assertion with scope "JMeter Variable":

enter image description here

enter image description here

enter image description here

enter image description here

Upvotes: 1

Dmitri T
Dmitri T

Reputation: 168157

Configure your JSON Path Assertion as follows:

  • JSON Path: [?($.core == true && $.matching == true)]
  • Tick Validate against expected value box
  • Expected value: []
  • Tick Invert Assertion box

JMeter JSON Path Assertion

References:

Upvotes: 2

Selva
Selva

Reputation: 541

You should be able to use the BeanShell assertion and the built-in JSON parser to achieve this.

import net.minidev.json.parser.JSONParser;
import net.minidev.json.JSONObject;
import net.minidev.json.JSONArray;


try {

    // Use JSONParser to parse the JSON
    JSONParser parser = new JSONParser(JSONParser.ACCEPT_NON_QUOTE|JSONParser.ACCEPT_SIMPLE_QUOTE); 
    JSONObject rootObject = (JSONObject) parser.parse(prev.getResponseDataAsString());

    if ( rootObject.get("matching") && rootObject.get("core") ) {
         Failure=false;
    }
    else {
         Failure=true;
    }


}
catch ( Exception ex) {
    log.info("Exception.." + ex);
     Failure=true;
}

Upvotes: -1

Related Questions