Search for a pattern and extract values in a json array

My question:
Hi all, I’m trying to match a pattern in json response array, perhaps it is not a straight forward text match.
Let me describe the challenge I have.

I have the following json as response:
[
{
“description”: " (1997-2000) 1234vol",
“ID”: “486”,
“addendum”: “1”
},
{
“description”: " (2000-2001) 1234vol",
“ID”: “486”,
“addendum”: “2”
},
{
“description”: “4T (2008-2018) 1234vol”,
“ID”: “1324”,
“addendum”: “1”
},
{
“description”: “4T (2018-2021) 1234vol”,
“ID”: “1324”,
“addendum”: “2”
},
{
“description”: “4T UBS (2018-2021) 1234vol”,
“ID”: “8745”,
“addendum”: “1”
},
{
“description”: “Bat (2001-2004) 1234vol”,
“ID”: “387”,
“addendum”: “1”
},
{
“description”: “Cat (2003-2004) 1234vol”,
“ID”: “387”,
“addendum”: “2”
},
{
“description”: “Cat (2004-2007) 1234vol”,
“ID”: “387”,
“addendum”: “3”
},
{
“description”: “Val (2007-2012) 1234vol”,
“ID”: “387”,
“addendum”: “5”
},
{
“description”: “Cat (2012-2018) 1234vol”,
“ID”: “387”,
“addendum”: “5”
},
{
“description”: “Nasty (2013-2017) 1234vol”,
“ID”: “4970”,
“addendum”: “1”
}
]

and I have ID and Year already declared in collection variables.

Now what I’m strugling with is to identify the right description.

E.g.
(“ID”,387) and (“Year”,2008)

So far I managed to match the ID in the array

if (el.ID===(pm.collectionVariables.get(“ID”))) {
pm.collectionVariables.set(“Description”,(el.description))
pm.collectionVariables.set(“Addendum”,(el.addendum));
return;
}
});

perhaps this will always end up with selecting the description “Cat (2012-2018) 1234vol”

I cannot figure out how to match the description “Val (2007-2012) 1234vol” that would be the right one given the input variable is year 2008

Postman uses JavaScript under the hood.

You can achieve what you want by using the JavaScript find function.

const response = pm.response.json();

console.log(response);

let search = response.find(function (obj) {
    return obj.description.includes("2018") && obj.ID === "387";
});

console.log(search);

pm.test("search is not undefined", () => {
    pm.expect(search).to.not.eql("undefined");
});

pm.test("ID = 387", () => {
    pm.expect(search.ID).to.eql("387");
});

pm.test("description includes 2008", () => {
    pm.expect(search.description).to.include("2018");
});
1 Like

Sorry, I just realised that you are not searching for 2018 specifically but that its within those dates.

That is much harder to achieve.

My recommendation would be that you loop through the response, and create two new elements with the date range. You can add these to the parsed response object.

From there, you could then update the filter to check between those years.

** EDIT **

The following should work.

const response = pm.response.json();

// define the ID and year for the search
let ID = "387";
let year = 2008;

// initial response
console.log(response);

// loop through each record and add start and end dates
response.forEach(obj => {
    let description = obj.description;
    let dates = (/\(([^)]*)\)/.exec(description)[1]).split("-");
    obj.startDate = parseInt(dates[0]);
    obj.endDate = parseInt(dates[1]);
});

console.log(response); // should now include start and end dates

// now we can search between the dates
let search = response.find(function (obj) {
    return obj.startDate <= year && obj.endDate >= year && obj.ID === ID;
});

// and the results are in
console.log(search);


// some example tests to consider.
pm.test("search is not undefined", () => {
    pm.expect(search).to.not.eql("undefined");
});

pm.test(`ID = ${ID}`, () => {
    pm.expect(search.ID).to.eql(ID);
});

pm.test("Correct Dates", () => {
    pm.expect(search.startDate).be.below(year);
    pm.expect(search.endDate).to.above(year);
    // maybe change this to.be.at.least(year)
});
1 Like

Thank you 1K for suggestion and code!
It worked perfectly!