Hi @taehoshino Thanks for nice welcome
I tried to use your way, but I think I can’t use it in my functions.
Look, I created two function one is to iterate through json file, and if it is at the “end” and this path is in array with paths to fields which I want to test, run function to create variable and create variable:
checkFile = {
myFunc: function checkFile(file, path, arrayWithPath)
{
path = path || 0;
var mainPath = path;
for(key in file)
{
if(mainPath === 0)
{
path = key;
}
else
{
path = mainPath + "." + key;
}
if(typeof file[key] != 'object')
{
if(arrayWithPath.includes(path))
{
createVariable.myFunc(path, file[key]);
}
}
else
{
if(Object.prototype.toString.call(file[key]).match(/\[\w+ (\w+)\]/)[1].toLowerCase() === 'array')
{
var array = file[key];
for(i = 0; i < array.length; i++)
{
if(typeof array[i] === 'object')
{
checkFile(array[i], path, arrayWithPath);
}
else
{
if(arrayWithPath.includes(path))
{
createVariable(path, array[i]);
}
}
}
}
else
{
checkFile(file[key], path, arrayWithPath);
}
}
}
}
}
createVariable = {
myFunc: function createVariable(variableName, variableValue)
{
var variable = pm.globals.get(`${variableName}`);
if (typeof variable == 'undefined')
{
var variable = new Array();
variable.push(variableValue);
pm.globals.set(`${variableName}`, variable);
}
else
{
variable.push(variableValue);
let finallVariable = [...new Set(variable)];
pm.globals.set(`${variableName}`, finallVariable);
}
}
}
My idea to use it is that I paste this
checkFile.myFunc(response,0,arrayWithPath)
into test section in my request and in response I get json, and using this function I iterate thru this json i create variables for paths which I interested.
When this functions are in Pre-request this variables isn’t created, its work only when this two functions are in tests sections.
And I have no idea why it’s not working, because I can create variables from the prerequests