JMeter Compare JSON with JDBC Request - json

I have a JSON like below:
{
"abc":" hello",
"def":" hi"
}
I want to compare thus with JDBC request output. I have saved the output in variables "abc" and "def" so as to match my JSON. I am using groovy to compare. I tried using JsonSlurper but could not retrieve the elements in JSON by index so that I can iterate through the JSON response. Please help me on this.

Related

Extracting a JSON out of a string using JSONPath

I have Json data as follows:
{
"template" : [
"{
"Id": "abc"
}"
]
}
I am using JSONPath to extract data from the Json above. I would like to extract the "Id" data from the Json using JsonPath.
The problem I see is, the data is being treated as a string and not as a Json as shown below.
"{
"Id": "abc"
}"
If there were no double-quotes I could have used JsonPath as follows:
$.template[0].Id
But due to the double-quotes, I am unable to access the "Id" data. I suspect there is a way to access this data using JsonPath-Expression but I am pretty much a novice here and no amount of research helped me out with a resolution.
How do I treat it as a Json and not as a string using JsonPath? Kindly help me out here.
JSON Path isn't going to be able to parse JSON that's encoded within a string. You need to perform three operations:
Get the string (use JSON Path or something else)
Parse the string as JSON.
Get the data you're looking for on that (JSON Path or something else)

Attaining the data in json format from the payload which is available as "org.mule.munit.common.util.ReusableByteArrayInputStream#53534c15" in mule 3

I need the real payload json data to be able to assert it against another hardcoded json file in munit (mule 3.9 and dataweave 1). The issue is the payload show as "org.mule.munit.common.util.ReusableByteArrayInputStream#53534c15" under payload. When I convert it to java I can see the data, but not in json format. How can I extract the json in this byte array stream to be able to assert it against a json hardcoded file.
I resolved it by using the "Byte to String" block
Then, I have added the "Assert Equals" block, but made sure to format both values likes this.
#[payload.replaceAll("\\s+","")]
#[getResource('sample.json').asString().replaceAll("\\s+","")]
This did exactly what I needed.

Filter partial JSON extractor response and send it as a request

I am working on the Rest API performance testing and I'm handling the JSON responses for the requests. How to split the response partialy and send it to the next request
I did use the online tool for JSON extractor but nothing helped.
Sampler (Api) Response :
{
"ResponseCode": "200",
"Response" : "PON CFF2345 893939",
"Message" : null
}
Based on the above response i used the JSON Extractor post processor and the json path expression as ($.Response)
Actual result : "PON CFF2345 893939"
But I want to extract the value only CFF2345 and send that value to the next sampler
You need to add after JSON extractor a Regular Expression extractor
Choose JMeter Variable Name to use the JMeter variable you saved
Use (\w+) as Regular Expression Template $1$ and Match No. 2 to get second string

Jmeter parse and Assert Array of Json objects

In Jmeter when trying to extract data from Json object everything is ok and works great but, when i have array of Json objects can't extract it.
Result is:
[{"id":1,"name":"test"},{"id":2,"name":"test2"}]
it is my project JSON Extractor and JSR233 Assertion.
Inside Groovy script i'm making log.info but it doesn't captures variable value which is described inside JSON Extraxtor.
String id = vars.get("id");
log.info ("The example answer is " + id);
if (id == ""){
AssertionResult.setFailureMessage("The id is null");
AssertionResult.setFailure(true);
}
Please note that if Json response looks like this {"id":1,"name":"test"}
everything works correctly.
Change your JSON Path Expression to look like: $..id. .. is a deep scan operator so it will return all id attributes values.
Change Match No to -1
It will result in the following variables:
id_1=1
id_2=2
id_matchNr=2
I have no idea what exactly you need to assert, hopefully you will be able to amend your Groovy script yourself.
Also be aware that there is JSON Assertion test element available since JMeter 4.0 so you won't need to have separate extractor and assertion elements.

JMeter JSON Path Extractor expression issue while searching arrays

My HTTP request returns an array similar to
[
{"id":"1", "data":"abc"},
{"id":"2", "data":"def"},
{"id":"3", "data":"ghi"}
]
As an input for a ForEach Controller I'm trying to extract the ids of above result into an array using a JSON Path Extractor with a Path expression of $..id.
I'm expecting to get something similar to ["1", "2","3"] but the output I get is
ids=3
ids_matchNr=3
It appears that the JSON Path Extractor only matches/extracts the last occurrence of id instead of collecting all occurrences of the id values and adding them to an aray.
The same expression and data return the expected ["1", "2","3"] result array in jsonpath.curiousconcept.com so the expression seems to be correct.
Am I doing something wrong or is this a bug in the JSON Path Extractor?
Below are some screen prints for a little test
I cannot reproduce your issue:
JSON Extractor
JSON Path Extractor
So double check you are using the latest version of JMeter and JSON Path Extractor, the latter one can be upgraded using JMeter Plugins Manager