JMeter pass JSON response value to next request - testing

I am using JMETER to test a web app.
First I perform a http GET request which returns a JSON array such as:
[
{
"key1":
{
"subKey":
[
9.120968,
39.255417
]
},
key2 : 1
},
{
"key1":
{
"subKey":
[
9.123852,
39.243237
]
},
key2 : 10
}
]
Basically I want to take randomly one element, take the elements of key1 and create 2 variables in JMeter that will be used for the next query (if randomly it is not possible than just the 1st element).
I tried using JSON Extractor with the following settings (the example shows a single variable case):
and in the next http GET request referencing the parameter as ${var1}.
How to set JSON Extractor to extract a value, save into a JMeter variable to be used in the next http GET request?

Correct JSON Path query would be something like:
$..key1.subKey[${__Random(0,1,)}]
You need to switch Apply to value either to Main sample only or to Main sample and sub-samples
In the above setup:
Match No: 0 - tells JMeter to get random value from key1 subkey
${__Random(0,1,)} - gets a random element from the array, i.e. 9.120968 or 39.255417
More information:
Jayway Jsonpath
API Testing With JMeter and the JSON Extractor

"JMeter variable name to use" option that you've switched on there means that you'd be examining the content of this variable INSTEAD of Sample result.
So the fix is obvious: if you intend to extract whatever you extracting from Sample result - change it back to it.
PS If you intend the opposite (process the variable content, not the sample result) - let me know please.

Related

Karate DSL: How to pass the the data as A'BC in the scenario outline

I have sample JSON where all the values for the keys are defined in regex as "#(value)" and the values are dervied from the data table in the feature file. (represented in Example in scenario outline)
eg:- {"key": "#(value)"}
I have one of the record which is like A'BC but when I pass this as it is the JS is getting failed with error as : Missing close quote
Example:-
| Value |
| A'BCD |
I tried passing the value with single slash \ in between A and 'BC but it is adding the double slash in the constructed JSON request
JSON constructed :
{"key": "A\'BCD"}
I need the the JSON to be like
{"key" : "A'BCD"}
Anyone can help please?
Maybe you should use a strategy to "post process" the CSV. Refer: https://stackoverflow.com/a/72395151/143475 and Reading csv file converts it to json but it takes every row in it as a string in karate
Also be aware of ways to improve dynamic scenario outlines coming in the next version: https://github.com/karatelabs/karate/issues/1905

Running into `org.graalvm.polyglot.PolyglotException: ReferenceError` while using JSON Array Data Source formed using Karate.mapWithKey

I am trying to pass a JSON Array Data Source to form a dynamic path for my API tests.
I am converting an existing Array (which I get back from another APIand JS function) to a JSON Array using karate.mapWithKey in the same feature file.
When I print the value of input, I do see a properly formatted list of objects as below
[
{
"keyUUID": "1234"
},
{
"keyUUID": "5678"
}
]
However, when I run the whole feature, I am seeing an error as org.graalvm.polyglot.PolyglotException: ReferenceError: "input" is not defined
Feature: Data-Driven Feature
Scenario Outline: looping over list of json objects
* def keys = ['1234','5678']
* def input = karate.mapWithKey(keys,'keyUUID')
* print input
* When path is <name>
Examples:
| input |
Am I missing something here?
I tried reading through a json file , which works fine but the json array of objects created using mapWithKey always gives an error.
I was able to resolve it after moving the input [json array of objects creation] from within scenario outline to Background
Feature: Data-Driven Feature
Background:
* def keys = ['1234','5678']
* def input = karate.mapWithKey(keys,'keyUUID')
* print input
Scenario Outline: looping over list of json objects
* When path is <name>
Examples:
| input |
The reference error I was receiving earlier for input is resolved now.

Need Pentaho JSON without array

I wanted to output json data not as array object and I did the changes mentioned in the pentaho document, but the output is always array even for the single set of values. I am using PDI 9.1 and I tested using the ktr from the below link
https://wiki.pentaho.com/download/attachments/25043814/json_output.ktr?version=1&modificationDate=1389259055000&api=v2
below statement is from https://wiki.pentaho.com/display/EAI/JSON+output
Another special case is when 'Nr. rows in a block' = 1.
If used with empty json block name output will looks like:
{
"name" : "item",
"value" : 25
}
My output comes like below
{ "": [ {"name":"item","value":25} ] }
I have resolved myself. I have added another JSON input step and defined as below
$.wellDesign[0] to get the array as string object

Pentaho JsonInput GET fields

I'm trying to use PDI to read data from an API (json) and now I'm simply trying to use json input to get a few specific fields but the get fields button on the input step gives me.
ERROR (version 8.3.0.0-371, build 8.3.0.0-371 from 2019-06-11 11.09.08 by buildguy) : Index 1 out of bounds for length 1
all the steps execute fine, and produce data - just not the json input step doesn't wnat to give me the fields option! - I've tired the text file and json oput and both write valid json so IDK whats going on....
PS. this is my first time using PDI
ISSUE 2:
It looks like PDI uses jayway for its json path parsing so I've been using this site https://jsonpath.herokuapp.com/ jayway selection which gives me my expected path. When I put that into the 'fields' of the json input dialog I only get the FIRST instance of that path value vs it actually parsing the json and giving me every instance, and can't figure out why though I assume it has something to do with PDI's row based view on things but I also don't know how to get it to understand that its json and it should be giving me back all values that match that path.
UPDATE 1:
I've been looking at this https://forums.pentaho.com/threads/135882-Parsing-JSON-data-without-knowing-field-names/ it seems like this Modified Java Script Value step might be the way to go. Will continue testing.
UPDATE 2
OK - Used the MJSV as posted above along with a select fields step and finally able to get the key's
var obj = JSON.parse(mydata);
var keys = Object.keys(obj);
for (var i = 0; i < Object.keys(obj).length; i++) {
var row = createRowCopy(getOutputRowMeta().size());
var idx = getInputRowMeta().size();
row[idx++] = keys[i];
putRow(row);
}
trans_Status = SKIP_TRANSFORMATION;

Jmeter Parsing specific id in another request

I need to fetch id parameter from JSON and parse that id value in another request URL. Issue is in JSON i am getting multiple values of id like 6, Etc/UTC etc. I want only integer value to pass.
Please suggest solution for automate this instead of passing value everytime.
You can add regex expression to you JSON expression
$..book[?(#.author =~ /.*REES/i)] All books matching regex (ignore case)
In your case add regular expression for numbers:
=~ /.*(\d+)/i)]
My expectation is that you need to amend your JSON Path query to fetch not all IDs but only the ID(s) which is(are) numeric. Unfortunately without seeing your response and JSON Path query we cannot suggest anything meaningful.
Looking into your response I can only suggest only getting the first numeric ID using JSR223 PostProcessor and Groovy language.
Add JSR223 PostProcessor after the JSON Extractor
Put the following code into "Script" area:
for (int i = 1; i <= (vars.get('id_matchNr') as int); i++) {
def currentVar = vars.get('id_' + i)
if (currentVar =~ '(\\d+)') {
vars.put('someid', currentVar)
break;
}
}
You will be able to access the first numeric ID as ${someid} where required.