Extract JSON value based on condition using Jmeter from an HTML page - jmeter

I want to extract JSON value based on condition using jmeter from an HTML page.
<!doctype html>
var list_data_obj = [{
"list": [
{
"field1": "123",
"field2": "xyz"
},
{
"field1": "456",
"field2": "abc"
},
{
"field1": "789",
"field2": "asdf"
}
],
"other1": "qwerty",
"other2": "asdfgh"
}]
</html>
have tried JSON Path Extractor as below but returned NULL.
$.list[?(#.field2 == 'abc')].field1
Thanks

You won't be able to use JSON Path Extractor as response data is not a valid JSON, it looks like a mix of HTML and JavaScript therefore you will need to use Regular Expression Extractor instead.
The relevant Regular Expression will look like:
"field1": "(.+?)",\s.*"field2": "abc"
References:
JMeter: Regular Expressions
Using RegEx (Regular Expression Extractor) With JMeter
Perl 5 Regex Cheat sheet

The key is to first extract a valid json using a regular expression, then apply a json path extractor on the previously extracted json.
You can perform a 2-step extraction to do this:
First Extract the Json from the page by performing a regexp extractor with "list": ([^^]+}\]), group $1$,
Then perform a Json Extractor on the extracted variable by selecting JMeter variable, then apply your json path $.list[?(#.field2 == 'abc')].field1.
Useful resources:
How to extract data from Json with JMeter

Related

JMeter: Extract Multiple values from JSON responses and store it in variable(comma seperated)

Can you please let me know efficient way to extract all ItemID from below Json response and store extracted values in a variable with coma separated.
Example: JsonResponse of a request.
[
{
"ItemID": "ITM40400002",
"deliveryId": "1",
},
{
"ItemID": "ITM40400003",
"deliveryId": "2",
},
{
"ItemID": "ITM40400002",
"deliveryId": "3",
}
]
Extracted Variable
Items = ITM40400002,ITM40400003,ITM40400002
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. foo
JSON Path Expressions: $..ItemID
Match No: -1
Tick Compute concatenation var
That's it, the JSON Extractor will extract all the ItemID attributes values and store them into foo_ALL JMeter Variable
Demo:
More information: API Testing With JMeter and the JSON Extractor

How to extract objects in Jmeter using JSONPath extractor

Hello i am using Jmeters JSONPath extractor, and the i am trying to extract certain fields from a response which is structured like the JSON below. I am trying to only extract the names of the child objects and not the details inside them, so something like:
Result[0]= key1
Result[1]= key2
Result[3]= key3
Would I be able to do this using JSONPath extractor
{
"output":{
"key1":{
"field1": "value1",
"field2": "value2"
},
"key2":{
"field1": "value1",
"field2": "value2"
},
"key3":{
"field1": "value1",
"field2": "value2"
}
}
}
I don't think it's even possible with JSON Path Extractor, however it's very easy achievable with JSON JMESPath Extractor which provides keys function fully covering your use case, the relevant query is as simple as:
keys(output)
Demo:
More information: The JMeter JSON JMESPath Extractor and Assertion: A Guide

I need to get the value of token id using Json path extractor instead of using Regex

I need to get the value of token id using JSON path extractor instead of using Regex
Here is the response:
{
"username":"Test",
"project_name":"Testing",
"user_domain_id":"default",
"roles":[
{
"name":"_member_"
}
],
"services_region":"regionOne",
"user_domain_name":"Default",
"enabled":true,
"domain_name":null,
"id":"9354ad0e4022423db85fa148c9876d30",
"available_services_regions":[
"regionOne"
],
"is_superuser":false,
"token":"6265e8da4807429ea65febf0a2312091",
"project_id":"5abf103686584fa09860f04d0887f181",
"domain_id":null
}
JSON Extractor is the best way to get value from JSON, in your case token is in first level child of JSON, so use the following expression:
$.token
Another way is using expression:
['token']
for more expression syntax see JSON Path
. or [] child operator

JMeter JSON Path Extractor how-to

I want to pull out href values where id : 923 using JSON extractor for Jmeter.
I tried,
$..entries[?(#.id == '923')].links[0].href
But it didn't work. How should I get the values by their id?
Here's some sample data.
"entries":[{
"id":"921",
"updated":"9999-12-31T23:59:59.999Z",
"links":[{
"href":"url1",
"rel":"related-action",
"title":"Execute related action"
},{
"href":"url2",
"rel":"icon"
}
]
},{
"id":"922",
"updated":"9999-12-31T23:59:59.999Z",
"links":[{
"href":"url3",
"rel":"related-action",
"title":"Execute action"
},{
"href":"url4",
"rel":"icon"
}
]
},{
"id":"923",
"updated":"9999-12-31T23:59:59.999Z",
"links":[{
"href":"url5",
"rel":"related-action",
"title":"Execute action"
},{
"href":"url6",
"rel":"icon"
}
]
}
]
Try removing quotation marks so your JSON Path Expression would look like:
$..entries[?(#.id == 923)].links[0].href
Your JSON is malformed, you need to surround it with {} characters in order so JSON Path Extractor could work with it.
Try updating Extras with Libs set plugin to the latest version - 1.3.0 as for the moment.
References:
JSON Path Syntax
Parsing JSON

jmeter regular expression extracting specific

I want to extract : children from "name":"recordInstanceId" from the following JSON.
The output should give me "lQBfjAu....P0tk" . How do I do it using Regex Extractor?
{
"name":"recordTypeView",
"attributes":{
"xmlns":""
},
"children":["all"
]
},{
"name":"isRuleBacked",
"attributes":{
"xmlns":""
},
"children":["false"
]
},{
"name":"recordInstanceId",
"attributes":{
"xmlns":""
},
"children":["lQBfjAu....P0tk"
]
}
If you need to extract lQBfjAu....P0tk from the following response:
{
"name": "recordInstanceId",
"attributes": {
"xmlns": ""
},
"children": [
"lQBfjAu....P0tk"
]
}
It could be done with JSON Path Extractor (available via JMeter Plugins) with a simple JSON Path Expression like:
$..children[0]
It isn't recommended to use Regular Expressions for dealing with JSON data, I would recommend extracting data from JSON responses with the JSON Path Extractor.
References:
JSON Path Syntax
Plugin installation instructions and XPath to JSON Path mapping
If you have any troubles - update your question to show complete response

Resources