jmeter regular expression extracting specific - jmeter

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

Related

JSON path for contains

I have the below json
{
"swagger": "2.0",
"info": {
"version": "v2",
"title": "Portfolio"
},
"host": "portfolio.com",
"schemes": [
"https"
],
"paths": {
"/v2/clients/{clientId}/assets": {
"get": {
"tags": [
"assets"
There are multiple paths I want to extract but they all start /v2/clients so what Im looking for is everything that starts with /v2/clients but none of the sub-data that sits the below, just the full paths between the ""
I am using jmeter JSON extractor and if I use $.paths it starts at this point but brings through all the mass amount of sub-data. I've tried looking around stackoverflow but can't find exactly what I am looking for. Any help appreciated
I believe the easiest would be going for JSR223 PostProcessor and Groovy language
Add JSR223 PostProcessor as a child of the request which returns above JSON
Put the following code into "Script" area:
def index = 1
new groovy.json.JsonSlurper().parse(prev.getResponseData()).paths.each { path ->
if (path.getKey().startsWith('/v2')) {
vars.put('path_' + index, path.getKey())
index++
}
}
That's it, you will have JMeter Variables like:
path_1=/v2/clients/{clientId}/assets
path_2=/v2/foo/bar
path_3=/v2/baz/qux
etc.
More information:
JsonSlurper
Apache Groovy: Parsing and producing JSON
Apache Groovy - Why and How You Should Use It

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

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

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 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

Resources