JMeter JSON Extract to extract random value which is not null - jmeter

I am using below JSON Extractor to extract random ItemID. It works fine but it also picks "null" value. Is there any way to extract random value from below json which is not null (i.e IT01 or IT02 in below example)
JSON path expression: $..ItemID
Match No (0 for Random) : 0**
[{
"ItemID": "null",
"deliveryId": "1",
}, {
"ItemID": "IT01",
"deliveryId": "2",
}, {
"ItemID": "IT02",
"deliveryId": "3",
}
]
Thanks

The following JSONPath expression will give you all ItemID attribute values which are not null:
$.[?(#.ItemID != "null")].ItemID
So you can get a random match by providing 0 as the "Match No."
More information:
JSONPath Operators
JSONPath Filter Operators
JMeter's JSON Path Extractor Plugin - Advanced Usage Scenarios

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

fetch the dynamic value from json response using regex or json extractor in Jmeter

From the following JSON response, want to fetch the value ref_id of both the type (now here the catch is the type place is dynamic), how to extract it using JSON or REGEX extractor or using post processer.
So if you see there are two ref_id, one is under metaData and one is under key. Want to select the which is under the meta data
for example for "type": "recall" the ref_id is cmVjYWxsLWU1ZGY4MTZkLTE0MTctNGJhNy1hMTQzLTY1OGJmMmYxYTRjMw== and for "type": "followUp" is Zm9sbG93VXAtMzk4MGRhZDQtOGE3Mi00YmM0LWFhYWEtM2Y3ZTU3NzZlNDM0.
And the position of "type": "recall" and "type": "followUp" is not constant in the json response. So I cannot use any constant index value to fetch this two value. Tried with "id": "(.*?)","type": "recall" and "id": "(.*?)","type": "followUp". But it is failing.
{
"entities": {
"cmVjYWxsLWU1ZGY4MTZkLTE0MTctNGJhNy1hMTQzLTY1OGJmMmYxYTRjMw==": {
"ref_id": "cmVjYWxsLWU1ZGY4MTZkLTE0MTctNGJhNy1hMTQzLTY1OGJmMmYxYTRjMw==",
"type": "recall",
"metaData": {
"ref_id": "e5df816d-1417-4ba7-a143-658bf2f1a4c3",
"accountId": "ef757dba-f0d5-4464-a338-4a810e02bf47",
"patientId": "e74a1b0f-d3e0-4b78-bc4b-83687786466e",
"timelineDate": "2021-01-30T14:28:24.738Z"
}
},
"Zm9sbG93VXAtMzk4MGRhZDQtOGE3Mi00YmM0LWFhYWEtM2Y3ZTU3NzZlNDM0": {
"ref_id": "Zm9sbG93VXAtMzk4MGRhZDQtOGE3Mi00YmM0LWFhYWEtM2Y3ZTU3NzZlNDM0",
"type": "followUp",
"metaData": {
"ref_id": "3980dad4-8a72-4bc4-aaaa-3f7e5776e434",
"patientId": "e74a1b0f-d3e0-4b78-bc4b-83687786466e",
"createdAt": "2021-01-29T14:36:15.127Z",
"timelineDate": "2021-01-29T14:36:15.127Z"
}
}
},
"result": [
"cmVjYWxsLWU1ZGY4MTZkLTE0MTctNGJhNy1hMTQzLTY1OGJmMmYxYTRjMw==",
"Zm9sbG93VXAtMzk4MGRhZDQtOGE3Mi00YmM0LWFhYWEtM2Y3ZTU3NzZlNDM0"
]
}
If you need just to get these ref_id attributes values from the response it can be done using JSON JMESPath Extractor, the expression is as simple as:
entities.*.ref_id
Demo:
More information:
JMESPath Tutorial
The JMeter JSON JMESPath Extractor and Assertion: A Guide
Could you please try this, as a JSON extractor.
$..[?(#.type == "recall")].ref_id
$..[?(#.type == "followUp")].ref_id

I need to extract value from JSON response only if the key "has_products" = true . Is it possible to extract in Jmeter?

My Json Response is as below:
{
"id":2,
"parent_id":1,
"has_products":false,
"has_categories":true,
"image":"",
"image_thumbs":[ ],
"name":"Default Category",
"categories":[
{
"id":3,
"parent_id":2,
"has_products":false,
"has_categories":true,
"image":"https:\/\/storage.googleapis.com\/gcs-rezolve-commerce-engine-dev-media\/1%2F26%2Fcategory%2Fimages_7_.jpg?GoogleAccessId=rcedev#ferrous-layout-183111.iam.gserviceaccount.com&Expires=1515750870&Signature=BUVCdagE09Do%2B%2B6S9FXzmGUCJQP4QYHvoOuKOuLUQz9pJGwtwtkVF6h%2BuTzo5Jdqv%2F%2B4ACIw8WCWoT%2B8KQgNDpXA%2FCY2dkCDsfxb1VRXS0ff5dp4%2F2cq3WjE%2B8biudDXvhQvSWYx%2FyS%2FAYKkK%2FIXRqQ%2FlimDIYUR%2BRgdH6aEze4FNW5vCtGAHK%2BlscQ9GSFeG%2BN6KxMwFbjNpNNZT8KZ%2BLpdKbiHca%2Fk86TeYkmnvICdHpnaDqQJHGbofH%2BFPcbOWnjVCn9uD4gJQnfS4Y7a4OWdhG1q60Kr6KNpKmSOhOcD1P1NHFTn8SYI6GemGJPUXZ8Y3A40etORw1yfJC4M4Q%3D%3D",
"image_thumbs":[
],
"name":"Home Decors",
"categories":null
},
{
"id":4,
"parent_id":2,
"has_products":false,
"has_categories":true,
"image":"https:\/\/storage.googleapis.com\/gcs-rezolve-commerce-engine-dev-media\/1%2F26%2Fcategory%2F332034-jewelry.png?GoogleAccessId=rcedev#ferrous-layout-183111.iam.gserviceaccount.com&Expires=1515750870&Signature=OxdyQ6mYX5Shoz4pBf40MZr9I9PFdYUqD3Rihbn8EOpbj%2FekHSFiWGdUYZ1HeZmpB9mZ49qhKh3b0E25H%2FRPVvzv6MURzYOg4EfRa66KrjFAQVP5WbgC5dVzpKc2Nh8FWn4%2FgA70gQ1mE3Uh1KF62Tw2aFvlbughPM5ijC2j35xHTAYcnNhr2Pcx4%2B9%2BWVHecXCdvw%2Fy3CMrgmZGZFSfALNBsiSM8IKKtRXwqHqVRg9CwutbaVL5zDd0GbTdBhwZLlJxqAx9Z1RE4l78Rv3bA2BeLhk5OqX2ajU5YYEQC69tTm0Mq9jzNF7%2F9zhtwCsqDfiygmdMKs9D7vDqD0xD%2Fg%3D%3D",
"image_thumbs":[
"https:\/\/storage.googleapis.com\/gcs-rezolve-commerce-engine-dev-media\/1%2F26%2Fcategory%2F332034-jewelry_thumb_400x376.png?GoogleAccessId=rcedev#ferrous-layout-183111.iam.gserviceaccount.com&Expires=1515750870&Signature=bOX4Z8EZwHb80ZNWtJ%2BrTq5hkUMNkTMGrtjk%2BvEyJ9H0XSDUMsmZVgMjgZVprgc23oLBR2SCDGvLNJKkCij2GIX1X9HT7QCFt4uLgvFy%2FmATHDcI9AOL55P8ypv7GJHBeUU7jdJKl5o641Uvf2s22yTCZpJenIiJCzJyxGAN%2BRV%2FwyD0OXkcTYjgwpJ5zi6SK%2BOYY5EGfTqFQBEbuG9WUAvNgeY3fCFgWoRgrp2XuxM5meXlcT7WA%2FZzt6wougjimBQsdF03s04Kq3cR6jsiL6aZFjhQacToBtkLFF7xY30dQr%2FO0D3tgs57QJXdD45QPBX4dAhgV2fSo%2BDK4D1kwQ%3D%3D"
],
"name":"jewelry",
"categories":null
},
{
"id":10,
"parent_id":2,
"has_products":true,
"has_categories":false,
"image":"",
"image_thumbs":[
],
"name":"Ring Collection",
"categories":null
}
]
}
Can any one help me on this ?
Assuming that the value you want to extract is the id, then add a JSON Extractor with the below configurations:
Variable names : the name of the variable which will hold the value.
JSON Path expressions : $.categories[?(#.has_products == true)].id
Match Numbers : 1 // use -1 if you want to extract all id values which pass the condition.
Default values : NOT_FOUND
Reference:
advanced-usage-json-path-extractor-jmeter but use Core JMeter JSON Extractor instead of the plugin mentioned in blog

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

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

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