I need to extract value from JSON response only if the key "has_products" = true . Is it possible to extract in Jmeter? - 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

Related

JMeter - How to add data to request body in every loop?

I'm working with an API that returns response in the following format:
"products": [
{
"name": "ABC"
"id": "ABCDEFG"
"Status":Open
}
{
"name": "XYZ"
"id": "LMNOPQ"
"Status":Open
} ]
The number of products varies and so does the number of IDs generated. I need to extract all id values which I'm doing using a JSON extractor and setting the match number to -1.
I need to pass these ID values in this request:
"products": [
{
"id": "id1"
}
{
"id": "id2"
} ]
If there are 5 IDs then the request needs to contain 5 id values.
I've tried using loops but I can't figure out how to add a { "id": } to the request body on every iteration of the loop. Is there any way to simulate this?
Instead of using JSON Extractor you could do everything with JSR223 PostProcessor and extract the IDs and build the next request body in one shot.
Example code:
def ids = new groovy.json.JsonSlurper().parse(prev.getResponseData()).products.collect { product -> product.id }
def payload = [:]
def products = []
ids.each { id ->
products.add([id: id])
}
payload.put('products', products)
vars.put('payload', new groovy.json.JsonBuilder(payload).toPrettyString())
You will be able to refer generated value as ${payload} where required.
More information:
Apache Groovy: Parsing and producing JSON
Apache Groovy: What Is Groovy Used For?

JMeter JSON Extract to extract random value which is not null

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

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 get Number of rows in Jmeter using Json Path Extractor

My Response is:
{
rows{
["2","xxx","yyyy"],
["3","xxx","yyyy"],
["4","xxx","yyyy"],
}
}
I am using $.rows to get all the rows. as well I am giving $.rows[0].[1] to get the value from 1st row and 1st value..
I am trying to get the total number of rows using $.rows.size() or length. It is not following exception. How to get the number of rows?
Exception: Options AS_PATH_LIST and ALWAYS_RETURN_LIST are not allowed
when using path functions!
Given the following JSON:
{
"name":"John",
"age":30,
"cars": [
{ "name":"Ford", "models":[ "Fiesta", "Focus", "Mustang" ] },
{ "name":"BMW", "models":[ "320", "X3", "X5" ] },
{ "name":"Fiat", "models":[ "500", "Panda" ] }
]
}
If I add JSON Path Extractor as a child of the sample which returns the above JSON and configure it like:
Reference Name: arraySize
JSONPath Expression: $..cars.length()
I am able to see the following variables in the View Results Tree listener:
arraySize=[3]
arraySize_1=3
arraySize_matchNr=1
Which seems to be something you're looking for.
You can install JSON Path Extractor along with JSON Path Assertion using JMeter Plugins Manager

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