fetch the dynamic value from json response using regex or json extractor in Jmeter - 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

Related

Jmeter - How to get nested object in json with multiple object

I have this json:
{
"deviceId": "deviceCustom",
"moduleId": "custom",
"properties": {
"desired": {
"settings": {
"ef78c18c-2291-4d15-ae87-d89abb9b1fef": {
"name": "elements",
"version": "1.0.0",
"category": "A1"
},
"f4b04c94-4643-4b13-b10c-9a00fbf4ea27": {
"name": "tags",
"version": "2.0.0",
"category": "B1"
}
}
}
}
}
and I would like to get separately all the objects under "settings". E.g:
settings_1="f4b04c94-4643-4b13-b10c-9a00fbf4ea27":{"name":"tags","version":"2.0.0","category":"B1"}
settings_2="ef78c18c-2291-4d15-ae87-d89abb9b1fef":{"name":"elements","version":"1.0.0","category":"A1"}
settings_matchNr=2
In Jmeter I've configured a JSON Extractor with this JSON Path expression: $.properties.desired.settings but I got this result:
settings_1={"f4b04c94-4643-4b13-b10c-9a00fbf4ea27":{"name":"tags","version":"2.0.0","category":"B1"},"ef78c18c-2291-4d15-ae87-d89abb9b1fef":{"name":"elements","version":"1.0.0","category":"A1"}}
settings_matchNr=1
I've also tried to use JSR223 Post Processor with Slurper but no valid result.
Could you help me on that?
Thanks in advance.
Add JSR223 PostProcessor as a child of the request which returns the above JSON
Put the following code into "Script" area:
new groovy.json.JsonSlurper().parse(prev.getResponseData()).properties.desired.settings.entrySet().eachWithIndex { entry, index ->
def setting = [:]
setting.put(entry.getKey(), entry.getValue())
vars.put('setting_' + (index + 1), new groovy.json.JsonBuilder(setting).toPrettyString())
}
That's it, you will be able to refer the extracted JSON Objects as ${setting_1} and ${setting_2}
More information:
Apache Groovy - Parsing and producing JSON
Apache Groovy - Why and How You Should Use It

Jmeter - How to extract value from the request (not response) using RegEx

I have JSON request as following:
{
"type": "SIGNUP",
"data": {
"userAccountInfo": {
"email": "ta0620050706#gmail.com",
"password": "qweQwe123!"
},
"userAddressInfo": {
"country": "United States"
},
"userPersonalInfo": {
"firstName": "test",
"lastName": "test"
}
}
}
How can extract ta0620050706#gmail.com from the following request, considering the value of the email is always dinamic?
Any help is appreciated!
If you're talking about HTTP Request sampler and the above JSON is in the "Body Data" tab like:
You can extract the email by adding a JSR223 PreProcessor and using the following code there:
vars.put('email', new groovy.json.JsonSlurper().parseText(sampler.getArguments().getArgument(0).getValue()).data.userAccountInfo.email)
It will extract the value you're looking for and store it into ${email} JMeter Variable
More information:
Apache Groovy - Parsing and producing JSON
Apache Groovy - Why and How You Should Use It

Laravel Api resource controller json format

I managed to change the format for the json response of the laravel resource controller for APIs, i want to make a key -> value response but i cant find the way to remove the brackets:
the response i get is this:
{
"1": [
{
"updated_at": 1536147154
}
],
"2": [
{
"updated_at": 1536160598
}
]
}
but i want it to be like this:
{
"1":
{
"updated_at": 1536147154
},
"2":
{
"updated_at": 1536160598
}
}
I get the response from an eloquent collection, and then I group it by id, but I don't know how to get rid of the brackets because the values end in an array.
I don't know if I am clear in my question.
Solved it, i removed the groupby and instead, I used the keyBy to assign their id as a key.
Transformers can be used to format json
I used https://github.com/spatie/laravel-fractal to format my json response in controllers

Wrapping Data Structure in a data key API Blueprint / Apiary

So let's say I have a 200 response which body should be:
{
"data": [
{
"id": 1,
"title": "Activity 1"
},
{
"id": 1,
"title": "Activity 2"
}
]
}
I have managed to get this behavior of the response body by using this in API Blueprint.
+ Response 200 (application/json)
+ Attributes
+ data (array[Activity])
(Note that I can't add the data key to the data structure itself, because it's only present on the single response. If I need to nest an Activity inside another structure, it shouldn't have the data key.)
This doesn't seem right
The reason why I don't think it's the correct way of doing it, is beacuse of the JSON schema for this response which is:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"data": {
"type": "array"
}
}
}
Note how the actual activity is excluded.
How can I wrap my response in a data key properly, and have it reflected in both the body AND the schema?
You should use this line:
+ data(array[Activity], fixed-type)
The fixed-type keyword fixes the type of the items in the array.

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