Unable to consume pm.iterationData.get("String") into JSON find method when running as collection

var jsonData = pm.response.json();
let serviceName = pm.iterationData.get(“Service”);
//console.log(serviceName);

var id = jsonData[‘_embedded’][‘doc:solutionDefinitions’].find(item => item.name === serviceName).id;
//console.log(id);
pm.environment.set(“solutionDefinitionID”, id);

in above request , i am sending data from csv file from “Service” as column . when i am printing data after reading , able to get data , but while using it in find function its not working . same code is working with stand alone data ( without using itertaion ) … that means find method also working alone

console.log(serviceName);

What is that returning?

In the find, you have serviceName).id; which would indicate that its an object? With an “id” key in it. Is the ID contained in your CSV file? (Can you provide a screenshot of the CSV file?).

Does the following actually return anything. I would expect it to be undefined as I can’t see it being initialised anywhere in the script.

console.log(id);

If the ID is part of the service name, then it should be.

console.log(serviceName.id);

Thanks for Your reply . without using iterationData its working

i am searching id with the help of name . attached json.

name is being supplied with csv , attached csv as well .

cvsfile

I’m wondering if you need another set of brackets in there.

var id = (jsonData[‘_embedded’][‘doc:solutionDefinitions’].find(item => item.name === serviceName)).id;

However, as you say its working without the iterationData, I’m assuming that the find is actually ok.

Although I would probably break it down into two lines for troubleshooting.

let search = jsonData["_embedded’"["doc:solutionDefinitions"].find(item => item.name === serviceName); // special characters so using [] instead of dot notation.
console.log(search);
let id = search.id;
console.log(id);

You can also reference variables from the CSV file using the special data variable.

let serviceName = data.Service

Finally, does the console log for the serviceName work with the iterationData (or is that failing).

If you copy and paste your example response as text, I can reproduce this my end.

i am also tried like this , but no luck.

var jsonData = pm.response.json();
//var serviceName = “Rama Mohan”;
let serviceName = pm.iterationData.get(“Service”);
console.log(serviceName);
var sol = jsonData[‘_embedded’][‘doc:solutionDefinitions’]

var result = sol.find(obj => {
// Returns the object where the given property has some value
return obj.name === serviceName
})
console.log(result.id)

for same object want to find “id” property by using "name ".

Can you include an example response, and I’m pretty sure I can help with the find\search of the array.

Based on the following response.

{
    "_embedded": {
        "doc:solutionDefinitions": [
            {
                "id": "1107255c",
                "name": "Automation Test",
                "description": "A powerful tool documentation."
            },
            {
                "id": "1107255b",
                "name": "Dev Test",
                "description": "A powerful tool documentation."
            }
        ]
    }
}

The following appears to work…

const jsonData = pm.response.json()

let serviceName = data.Service // data will be an object with the current iteration data
// let serviceName = "Dev Test" 
console.log(serviceName); // "Dev Test"

let search = jsonData["_embedded"]["doc:solutionDefinitions"].find(item => item.name === serviceName);
console.log(search);

let id = search.id;
console.log(id); // 1107255b

image

Thanks for support. i have realised the error, its due to data where i am parsing. its not problem with find.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.