jmeter how to use json path extractor - jmeter

I'm new to jmeter.
I want to save the value of the key in a variable, if the id is 7106 from the following response
[{"id":"MAIN","key":1},
{"id":"87","city":"Roselle","key":1000,"state":"IL"},
{"id":"7102","city":"Chicago","key":110,"state":"IL"},
{"id":"7106","city":"Crown Point","key":110038,"state":"IN"},
{"id":"7108","city":"Indianapolis","key":1139,"state":"NJ"}]
How to do it in jmeter?

Here are some options:
Option 1: Regular Expression Extractor
Option 2: JSON JMESPath Extractor
Option 3: JSON Extractor

Add post Processor - JSON Extractor to your sampler and create a variable of any name, in JSON Path expression give value = $..key , this should extract all values that "key" holds.
Now add View Result Tree to see all variables created, suppose you give variable name as "key" , all values should be stored as key_1, Key_2 and so on..
you can use any value now, as in your case it would be ${key_4} . Adding picture for your help
JSON Extractor

The best option is to use JSON JMESPath extractor which allows executing arbitrary JMESPath queries, in your case it would be a Filter Expression like:
[? id=='7106'].key
Demo:
More information: The JMeter JSON JMESPath Extractor and Assertion: A Guide

Related

How can I save, Key value pair in string variable from response of sampler of Jmeter

I want to save two value as key value pair in List from previous sampler of Jmeter so that I can use that string in pre Preprocessor
Try the below steps:
Add the 'Regular Expression Extractor" from the Post-Processor
Define the required variables
Say, for a single variable set the fields in Regular Expression Extractor as below
Field to check => Body
Name of the created variable => username
Regular Expression => username=(.+?)
Template => $1$
Match No => 1
Now, Pass the variable in the HTTP request > Parameters
Say,
Name = Username
Value = {$username}
Run the test
Reference: https://www.redline13.com/blog/2018/09/jmeter-extract-and-re-use-as-variable/
It depends on the format of the data you're getting in the response and the format of the data you need to pass in the next request.
In order to get a comprehensive response you should provide at least partial response data and desired at least partial request body
Normally you should use a Post-Processor for extract data from the response:
For HTML - CSS Selector Extractor
For JSON - JSON Extractor or JSON JMESPath Extractor
For XML - XPath Extractor or XPath2 Extractor
For everything else - Regular Expression Extractor or Boundary Extractor
all of them extract values into JMeter Variables which can be used in the next requests

extract the a field value with same name from JSON using JMeter

In the JSON response I have multiple fields with same name inside different tags . But I need to fetch the from that tag where matches the number I want. Say in the below examplele I want to fetch the value "(786)402-9010" when customerPpid=467133011
[{"oopId":"110034477","timestamp":"3698652681958","targetType":"TMTroubleCall","billAccount":"3655732026","customerFormattedPhoneNumber":"(786)505-2911","customerIsRequestingCallBack":"false","customerPpid":"609188407","acceptTime":"03/16/2018 11:31:00","originator":"DPH0GNL","priorityForDisplay":"3","remarks":"test","ddbKey":"8614147890T","displayType":"SNCU","parentFplId":"268224478","parentActualDeviceType":"TXU","parentDdbKey":"8614147890T","parentTroubleCoordinateX":"863634","parentTroubleCoordinateY":"459622","parentPreviousProtectiveDeviceStack":"268221704,23282601,23281142","toldItr":"03/16/2018 14:30:00","toldMode":"N","ticketCallComplaints":[{"componentId":"1685289","description":"No Current"}],"customerLanguageMenuOption":"1"},
{"oopId":"114249429","timestamp":"3698652636567","targetType":"TMTroubleCall","billAccount":"6182150000","customerFormattedPhoneNumber":"(786)402-9010","customerIsRequestingCallBack":"false","customerPpid":"467133011","acceptTime":"03/16/2018 11:31:00","attachTime":"03/16/2018 11:31:00","originator":"DPH0GNL","priorityForDisplay":"3","remarks":"testing","ddbKey":"8614154820T","displayType":"SNCU","parentFplId":"268224477","parentActualDeviceType":"TXU","parentDdbKey":"8614154820T","parentTroubleCoordinateX":"864084","parentTroubleCoordinateY":"459307","parentPreviousProtectiveDeviceStack":"268221704,23282601,23281142","toldItr":"03/16/2018 14:30:00","toldMode":"N","ticketCallComplaints":[{"componentId":"1685289","description":"No Current"},{"componentId":"4063885","description":"Customer checked breaker"}],"customerLanguageMenuOption":"1"},
how should i do it?
Add JSON Extractor as a child of the request which returns the above JSON
Configure it as follows:
Names of created variables: anything meaningful, i.e. phone
JSON Path Expressions:
$..[?(#.customerPpid == '467133011')].customerFormattedPhoneNumber
That's it, now you should be able to access the extracted value as ${phone} where required.
Demo:
References:
JSON Path Operators
JMeter's JSON Path Extractor Plugin - Advanced Usage Scenarios
Use the following Json path expression
$..[?(#.customerPpid=="467133011")].customerFormattedPhoneNumber
Add a JSON extractor to the request as shown in the screenshot below
Please follow this Link for more info on extracting variables
Jmeter Json Extractor

How I can get a random value string in href by JSON Path Extractor with JMeter?

I have JSON responce
{
"sessionName":"eL7tYgxhYh",
"imageSrc":"/Common/CaptchaImage/**eL7tYgxhYh**?t=636573960197174426"
}
How I can get a random value string "eL7tYgxhYh" in href by JSON Path Extractor with JMeter?
You cannot achieve this using JSON Extractor as it can only deal with JSON Objects, i.e. you can easily get full value of the imageSrc attribute, but not more.
I would recommend switching to Boundary Extractor instead, the relevant configuration would be something like:
Name of created variable: anything meaningful, i.e. href
Left Boundary: CaptchaImage/
Right Boundary: ?
That's it, the "interesting" value should be now extracted and you should be able to refer it as ${href} where required
Demo:
More information: The Boundary Extractor vs. the Regular Expression Extractor in JMeter
JSON format is in key-value pair so you just need to mention the key for which you need to extract the value. As in your case, you can use following JSON path expression for fetching the text mentioned by you:
$.sessionName
You can also use regular expression extractor which is another most important element of JMeter to extract the dynamic variables. Please refer to below blog of RedLine13 for more information on Regular Expressions:
https://www.redline13.com/blog/2016/01/jmeter-extract-and-re-use-as-variable/
Let me know if you have any further question
Above figure shows how to access access_token response of json and store it in access_token variable. In your case need to replace below.
JSONPath Expression use
$..sessionName
Destination variable Name
SessionName
more information for accessing json path
To Get "636573960197174426"
To get required string from result string you can use BeanShell Assertion to split the string.
And Use link to fetch the vaiable properties and jmeter elements
To Fetch jmeter Elements

Passing variable from response to request in JMETER

In need to pass a data from a response to the subsequent request. Something goes wrong and the default variable value appears in the request.
First request returns the JSON in response body which looks like this:
{"issued_at":"2016-01-14T12:41:01.000Z","expires":"2016-01-14T12:46:01.000Z","id":"j6M ... MTA=="}
I extract the value of the id attribute using the Regular Expression Extractor:
Then I pass the token variable to the subsequent request parameter:
But the request is created with the default value of the variable:
There is a JSON Path Extractor designed to deal with JSON content type, I believe it would be easier to use it.
The relevant JSON Path query will be as simple as $..id
See Using the XPath Extractor in JMeter (scroll down to "Parsing JSON") for comprehensive information on plugin installation and usage and JSONPath - XPath for JSON for JSONPath language reference and examples.
In regards to your Regular Expression Extractor configuration:
remove 1 from Match No.
Provide $1$ as Template
If you look at the Regular Expression Extractor documentation, the field Template is required. I suggest you to use value $1$ and try again.
The problem was resolved by setting the "Field to check" radio button to "Body" at the "Regular Expression Extractor" dialog and by setting the Template field value to $1$.
Thanks to alphamikevictor and Dmitri T for help!
You should use ${token_g1} to get the value of the first group of the regex match (the value you're looking for).

Regular expression in Jmeter for a json reponse

How do i write the regular expression for the json response.
My json response is "publisher":"/api/web/publishers/194".
From this response i want to get only the numeric value i.e 194. Some one pls help in getting it?
If "194" is the only integer in your response than your regex can be limited to just '(\d+)`
If you response contains more digits, you'll need to be more specific, like
"publisher":"/api/web/publishers/(\d+)"
So something like:
Reference Name: publisher
Regular Expression: "publisher":"/api/web/publishers/(\d+)"
Template: $1$
Will extract "194" and store it in "publisher" JMeter Variable. You'll be able to refer to it as ${publisher} later in Thread Group.
Going forward if you'll need to extract something from more complex JSON structures I'd recommend consider using JSON Path Extractor available via JMeter Plugins - the extractor lives in "Extras with Libs Set" package.
See Using the XPath Extractor in JMeter guide (scroll down to "Parsing JSON") for more information and XPath to JSON mapping.

Resources