Regex to get a field of Json - json

I have a JSON text which is returned by an API. I need to grab the value of "id" field so I can use it in a test (I am doing a corrolate test in JMeter).
I can try and find the "id": text however I cannot get the Z3G2D93 part.
Regex:
/"id":"(.+?)"/g
JSON :
{
"req":{
"dat":{
"bt":"",
"ot":"07-Apr 08:21",
"typ":"PickUp",
"tot":"3480",
"ast":"",
"an":"Test Test",
"id":"Z3G2D93"
}
}
}

Configure Regular Expression Extractor as follows:
Reference Name: anything meaningful, i.e. id
Regular Expression: "id":"(.+?)"
Template: $1$
By the way, you can test your Regular Expressions in JMeter right in View Results Tree listener using RegExp Tester mode like:
See How to debug your Apache JMeter script article for more information on how to get to the bottom of JMeter script failure.
By the way, it might be easier to deal with JSON responses using JSON Path Extractor available via JMeter Plugins

Related

I am trying to assert if two strings of a response are same or not using response assertion in jmeter but it's showing an error

In response assertion I have given field to test as text response and pattern matching as contains
Pattern to test is
${bookmark_ALL}${value}=${value}${other_bookmark_ALL}
I have taken those bookmark values using json extractor given match -1 for it as well, both bookmark values are in string format
If you want to compare 2 JMeter Variables using Response Assertion the correct setup would be something like:
You can check JMeter Variables values using Debug Sampler
Also contains mode assumes regular expression, most probably you were looking for substring
More information: How to Use JMeter Assertions in Three Easy Steps

Need to extract a josn value for nested requests in JMeter using Jsonpath extractor

I have response which derived from a action. I need to extract a value of userid node using json path extractor in JMeter
{
"ABC": "{"response":{"userId":user1,"caseId":0,"name":"Json","email":"json#xyz.com","mobileNumber":"1223456789","countryCodeId":"1","countryCode":"+90","emailOTP":830782,"mobileOTP":301879,"mobileOTPString":null,"otpCreationDate":"2021-10-14T10:01:38.5802765Z","configOTPTimeOut":120,"redirectUrl":null,"isOTPExpired":false,"countryCodeValidMessage":null,"mobileNumberValidMessage":null,"offset":"00:00:00","postStatus":false,"postMessage":null,"id":null,"response":null,"isProceedToCreateMeeting":false},"successcode":0,"message":null}"
}
I have tried $..ABC.response.userId, it says NoMatch. what is the correct syntax to extract userId node which is present inside response
What you're showing us is not a valid JSON (you can check it yourself using i.e. https://jsonlint.com/ website or equivalent) therefore you won't be able use either JSON Extractor or JSON JMESPath Extractor
Unless it's a copy-paste issue you're limited to Regular Expression Extractor, example regular expression would be something like:
"userId"\s*:\s*(.+?)\s*,

How to retrieve the JSON value from multiple array response data using regular expression extractor in jmeter?

{
"response":
{
"responseData":
{
"createdDate":"2016-04-23 14:39:35",
"modifiedDate":"2016-04-23 14:39:35",
"catalogID":1009
}
}
}
Here is sample JSON data for reference. I need to extract the value of catalogID=1009. How to extract the value of catalogID?? can anyone please share your idea???
What is the regular expression to retrieve the catalogID??
Use the following regular expression extractor configuration:
Reference Name: variable name of your choice, i.e. catalogID
Regular Expression: "catalogID":(\d+)
Template: $1$
Refer extracted value as ${catalogID} where required.
Few tips:
You can use View Results Tree listener in RegExp Tester mode to test your regular expressions against real response:
See How to debug your Apache JMeter script article for more details.
There is JSON Path Extractor available via JMeter Plugins which allows using JSON Path language (more handy than regular expressions when it comes to JSON), for example the relevant query to get "catalogID" will be as simple as:
$..catalogID[0]
There is also an online JSON Path Expression Tester
If you want to use regex, use
"catalogID":(.+?)
or
"catalogID":(\d+) <-- For only digits
You can also install the plugin JSONPath for jmeter and use this instead
$.response[0].responseData[0].catalogID
More info
I have found the solution for this question.
Here you want to extract the integer value means then use this requalar expression like "catalogID":(.*?),
String means "catalogID":"(.*?)",
Lets do it!

Verify whole json response in jmeter by value or sort Json

I'm not using JMeter too often, and I've run into very specific issue.
My REST response is always "the same", but nodes are not in the same order due to various reasons. As well, I can't put here whole response due to sensitive data, but let's use these dummy one:
First time response might be:
{
"properties":{
"prop1":false,
"prop2":false,
"prop3":165,
"prop4":"Audi",
"prop5":true,
"prop6":true,
"prop7":false,
"prop8":"1",
"prop9":"2.0",
"prop10":0
}
}
Then other time it might be like this:
{
"properties":{
"prop2":false,
"prop1":false,
"prop10":0,
"prop3":165,
"prop7":false,
"prop5":true,
"prop6":true,
"prop8":"1",
"prop9":"2.0",
"prop4":"Audi"
}
}
As you can see, the content it self is the same, but order of nodes it's not. I have 160+ nodes and thousand of possible response orders.
Is there an easy way to compare two JSON responses comparing matching key - values, or at least to sort the response, and then compare it with sorted one in assertion patterns?
I'm not using any plugins, just basic Apache JMeter.
Thanks
I've checked using Jython, you need to download the Jython Library and save to your jmeter lib directory.
I've checked 2 JSONs with Sampler1 and Sampler2, on Sampler1 I've add a BeanShell PostProcessor with this code:
vars.put("jsonSampler1",prev.getResponseDataAsString());
On Sampler2 I've add a BSF Assertion, specifying jython as the language and with the following code:
import json
jsonSampler1 = vars.get("jsonSampler1")
jsonSampler2 = prev.getResponseDataAsString()
objectSampler1 = json.loads(jsonSampler1)
objectSampler2 = json.loads(jsonSampler2)
if ( objectSampler1 != objectSampler2 ):
AssertionResult.setFailure(True)
AssertionResult.setFailureMessage("JSON data didn't match")
Yoy can find the whole jmx in this GistHub
You will most probably have to do this with a JSR223 Assertion and Groovy.
http://jmeter.apache.org/usermanual/component_reference.html#JSR223_Assertion
http://docs.groovy-lang.org/latest/html/api/groovy/json/JsonSlurper.html
Note that if you know Python, you might look at using Jython + JSR223.
I would just set up 10 jp#gc - JSON Path Assertions. Documentation for figuring out JSON Path format is here and you can test how it would work here.
For your example you would the assertion (Add > Assertion > jp#gc - JSON Path Assertions), then to test the prop 1 put:
$.properties.prop1
in the JSON Path field, click the Validate Against Expected Value checkbox, and put
false
in the expected value field. Repeat those steps for the other 9 changing the last part of the path to each key and the value you expected back in the expected value field.
This extractor is jmeter add on found here.

Need to extract JSON data from a JMeter response?

In JMeter, I need to extract some fields (City, Classification, and Chain) from a JSON response:
{
"StoreCode": "111243",
"StoreName": "Spencer - Sec 14 Gurgaon",
"Address1": "Gurgaon-Sector-14",
"Address2": "NCR",
"Pin": "110000",
"City": "NCR",
"Classification": "Vol 4",
"Chain": "Spencers",
"Version": "20281",
"VisitType": "Weekly"
}
Can it be done using the regular expression extractor? Is there another option?
If this piece of JSON is the all the response - it makes sense to use Regular Expression Extractor.
If you receive larger or more complicated structure - it's better to use special JSON Path Extractor available through plugin. JSON Path expressions for your JSON response would be something like $.City, $.Chain, etc.
See "Parsing JSON" chapter of Using the XPath Extractor in JMeter guide for more details on JSON Path language and on how to install the plugin.
Very easy with the plugin mentioned. See this for example. Here is link to plugin.
My biggest thing to understand was the flow. In your jmeter test you need to have an httprequest that returns data (in this case json data). So running your test you'd see json in the Response Data tab if you have a View Results Tree listener going. If you have this right click on the HttpRequest you want data from. ADD => Post Processors => jp#gc - JSON Path Extractor. Inside that extractor, you can name it anything you want.
The variable name should be one you already have defined in a User Defined Variables config element. The JSON Path would start with a dollar sign, then a dot, then the name of the key you want the value for from your json. So for me: $.logId the value from ... "logId": 4, ... in my json. It will store the number 4 in my userdefined variable. The default value can be set to something you'd never see like -1 or null or false etc...
BTW you reference your variable in your tests with ${variablename}. If putting into json and its a string do "${variablename}". Hope it helps.
Lots of the way to find out with the help of regular expression. Sharing one of them.
"City": "(.*)",
"Classification": "(.*)",
"Chain": "(.*)",