Creating a request that compare the array data in 2 different request

Hello,

I want to create a request that will give the different values which 2. request(newer) response array has but 1. request(older) response array hasn’t in “asinList” array data.

Please see the example below. My desired result array should be :

"asinList": [B000BO052SASDFFX]

  1. request JSON response sample array;
"asinList": [
"B0845R3QCX",
"B00AVQCCCW",
"B000BO052S"]
  1. request JSON response sample array;
"asinList": [
"B0845R3QCX",
"B00AVQCCCW",
"B000BO052SASDFFX"]

Can you please assist me how to create this request? I am new to coding so any help is appreciated.

Thank you

If you start with this setup:

let arr1 = [
"B0845R3QCX",
"B00AVQCCCW",
"B000BO052S"]

let arr2 = [
"B0845R3QCX",
"B00AVQCCCW",
"B000BO052SASDFFX"]

Then you can do different kinds of operations.

This code will show you ALL differences between the two arrays:

let difference = arr1
                 .filter(x => !arr2.includes(x))
                 .concat(arr2.filter(x => !arr1.includes(x)))

console.log(difference);

But I think you were looking for ONLY the things in the second array that are not in the first array, so a simpler filter/includes check can help there:

let difference = arr2.filter(x => !arr1.includes(x));
console.log(difference);

If you’re getting these lists in different requests then you can save the response for array 1 in, say, a collection variable, and then recall that when you run the request for array 2.

If this is the same request being run, then maybe your whole test code could look something like this: (this code is untested since I can’t easily form the exact data you’re using)

let data = pm.response.json()
let newList = data.asinList
let prevList= JSON.parse(pm.collectionVariables.get("prevList"));
if (prevList!= null && prevList!= undefined && prevList!= []) {
  let difference = newList.filter(x => !prevList.includes(x));
  if (difference.length > 0) {
    // do something because you found something in newList that wasn't in prevList
  }
}

// store the new list for next time
pm.collectionVariables("previousList", JSON.stringify(newList))

Thank you so much for your answer. The explanation is pretty clear. Since my knowledge is not enough to execute all these advices I will ask more help and I will try to be more clear.

As you mentioned, these arrays are going to be extracted from 2 different requests and the array s will be compared to find out the the second request array has but first request array has not.

1- Here is the my 1. request :

{
    "avg30_SALES_gte": 1,
    "avg30_SALES_lte": 10000,
    "buyBoxSellerId": [
        "A2UUWCCR4G5EFA"
    ],
    "current_BUY_BOX_SHIPPING_gte": 0,
    "sort": [
        [
            "current_SALES",
            "asc"
        ]
    ],
    "productType": [
        0,1
    ],
    "perPage": 300,
    "page": 0
}

1. request response

{
    "timestamp": 1647990090084,
    "tokensLeft": 258,
    "refillIn": 27274,
    "refillRate": 5,
    "tokenFlowReduction": 0.9150000354275107,
    "tokensConsumed": 10,
    "processingTimeInMs": 145,
    "asinList": [
        "B00GHPAC1O",
        "B00PXL9TG2",
        "B006GP8SGG",
        "B00PXL9ZCK",
        "B00G3BRNZ0",
        "B0002Z9QRG",
        "B005SEKCN8",
        "B004BTBP9Q",
        "B005JVHIZ0",
        "B005JVE2HC",
        "B005SEKCOW",
        "B005JVE20O",
        "B00ELHT3PG",
        "B005JVE286",
        "B005JVE1S2",
        "B081Y63G64",
        "B000BO052S",
        "B000BNYMX2",
        "B00HSKW09Y",
        "B00JIOUZ68",
        "B00ELHT3M4",
        "B081K728C4",
        "B084QBSP9X",
        "B081K8447W",
        "B081K8MC2D",
        "B000BO22OW",
        "B00SVM7XS6"
    ],
    "totalResults": 27

Here is my 2. request :

{
    "avg30_SALES_gte": 1,
    "avg30_SALES_lte": 10000,
    "buyBoxSellerId": [
        "A2UUWCCR4G5EFA"
    ],
    "current_BUY_BOX_SHIPPING_gte": 0,
    "sort": [
        [
            "current_SALES",
            "asc"
        ]
    ],
    "productType": [
        0,1
    ],
    "perPage": 300,
    "page": 0
}

2. request response

{
    "timestamp": 1647990394924,
    "tokensLeft": 258,
    "refillIn": 23149,
    "refillRate": 5,
    "tokenFlowReduction": 0.9150000354275107,
    "tokensConsumed": 10,
    "processingTimeInMs": 441,
    "asinList": [
        "B00GHPAC1O",
        "B00PXL9TG2",
        "B006GP8SGG",
        "B00PXL9ZCK",
        "B00G3BRNZ0",
        "B0002Z9QRG",
        "B005SEKCN8",
        "B004BTBP9Q",
        "B005JVHIZ0",
        "B005JVE2HC",
        "B005SEKCOW",
        "B005JVE20O",
        "B00ELHT3PG",
        "B005JVE286",
        "B005JVE1S2",
        "B081Y63G64",
        "B000BO052S",
        "B000BNYMX2",
        "B00HSKW09Y",
        "B00JIOUZ68",
        "B00ELHT3M4",
        "B081K728C4",
        "B084QBSP9X",
        "B081K8447W",
        "B081K8MC2D",
        "B07KYWH2W8",
        "B000BO22OW",
        "B00SVM7XS6",
        "B07WWJPZGQ",
        "B084TRHM8J",
        "B016MEFS4K"
    ],
    "totalResults": 31

Lastly I have tried to create a variable for the first response but I was not able to get only array “asinList” and not able to recall the variable in the second request.
The test I used is :

let response = pm.response.json(),
    savedData = JSON.stringify(response);

pm.collectionVariables.set("savedData", savedData);

My plan is :

1- Running the first request every day and getting dynamic array daily. After that running the second request by testing the different values in “asinList” array that request 2 has but request 1 hasn’t.

Note : 1. and 2. requests are identical. Response data is changing depending on request running time.

Thank you again.
Please let me know if you need other info.

Since you want to run the same request, you’ll need to manually clear the collection variable since Postman won’t necessary know which request is the “first one” of the day or the “second one” of the day.

I didn’t see in your messages or questions what exactly you wanted to do with the content once you get a list of strings (if any) that are in response #2 that are not also in response #1

One thing you mentioned in your last response:

let response = pm.response.json()
let savedData = JSON.stringify(response.asinList)
pm.collectionVariables.set("savedData", savedData)

That should save your “asinList” attribute as your “savedData”, instead of the entire response.

My primary suggestion would be to use console.log to examine the difference array so it can show you the different ID values it found when it ran the second request. Beyond that, what exactly do you want to do with that data? Will that data be sent somewhere else within Postman, or is it just for visual checking?

We can certainly help you with specific questions, but I wasn’t very clear on what else you need assistance with here.

I want to get a list of ASINs as an email in json format.
Can you also please give me more details how to use console.log to examine the difference array so it can show me the different ID values.

Thank you

In my first answer, I showed how to generate a “difference” array, which should hold everything in result set 2 that did NOT exist in result set 1. Did that not work for you?