Searching response for dynamic value based on specific string

Hello,

I have this test case: I need to search response for ID (dynamic value) and store it in variable for reusing it in next request. To find this ID, I need to go through array objects, find specific string/value in element “source”, and then grab value of another element (“args”) of the same object.

My response looks something like this:

{
    "id": "d2392e98-bac8-4385-9343-d1360427a7cd",
    "status": "IngestionBatchUpdated",
    "createdAt": "2023-03-27T13:46:35.2994054Z",
    "updatedAt": "2023-03-27T13:46:35.2994476Z",
    "auditEvents": [
        {
            "timestamp": "2023-03-27T13:47:46.4708557Z",
            "event_type": "Information",
            "source": "AuditorService/ChangeStatus",
            "message": "Changing status from MetadataTransformed to MappingMetadata",
            "args": [
                "MetadataTransformed",
                "MappingMetadata"
            ]
        },
        {
            "timestamp": "2023-03-27T13:47:47.1998269Z",
            "event_type": "Information",
            "source": "AuditorService/ChangeStatus",
            "message": "Changing status from MappingMetadata to MetadataMapped",
            "args": [
                "MappingMetadata",
                "MetadataMapped"
            ]
        },
        {
            "timestamp": "2023-03-27T13:47:48.9671202Z",
            "event_type": "Information",
            "source": "AuditorService/ChangeStatus",
            "message": "Changing status from MetadataMapped to CreatingAsset",
            "args": [
                "MetadataMapped",
                "CreatingAsset"
            ]
        },
        {
            "timestamp": "2023-03-27T13:48:09.3269921Z",
            "event_type": "Information",
            "source": "BrandStoreServiceFacade/CreateAsset",
            "message": "Asset '52121300' created in TBS.",
            "args": [
                "52121300"
            ]
        },
        {
            "timestamp": "2023-03-27T13:48:09.3813078Z",
            "event_type": "Information",
            "source": "AuditorService/ChangeStatus",
            "message": "Changing status from CreatingAsset to AssetCreated",
            "args": [
                "CreatingAsset",
                "AssetCreated"
            ]
        },
        {
            "timestamp": "2023-03-27T13:48:15.2381473Z",
            "event_type": "Information",
            "source": "BrandStoreServiceFacade/CheckPermissionsForAsset",
            "message": "User has all needed permissions.",
            "args": null
        } 
      ]
}

So I need to find:

    {
        "timestamp": "2023-03-27T13:48:09.3269921Z",
        "event_type": "Information",
        "source": "BrandStoreServiceFacade/CreateAsset", // I' looking for this string: "BrandStoreServiceFacade/CreateAsset"
        "message": "Asset '52121300' created in TBS.",
        "args": [
            "52121300" // I need to store this in variable.
        ]
    },

Based on other topics I tried this code (and different variations of it) but without success:

pm.test(`Audit log should contains 'asset_id' and its value cannot be 'undefined' or 'null'.`, () => {
       
    _.each(pm.response.json(), (responseBody) => {  
    if (responseBody.auditEvents.source.include("BrandStoreServiceFacade/CreateAsset")) {
        pm.environment.set("asset_id", responseBody.auditEvents.args)
    }
    })

    pm.expect(pm.environment.get('asset_id')).to.not.be.oneOf([undefined, null]);

I think my problem is I provide wrong path to element (or element value) of an array. Could someone please help me with this?

This can be achieved with the JavaScript find function.

The “auditEvents” is an array, which is what you target with the find.
The args element is also an array (although it only appears to have one entry) so you have to target that with args[0]. (array indexes start at zero).

As per the following example.

const response = pm.response.json(); 

// bring back the whole Audit Event
let searchV1 = response.auditEvents.find(event => {return event.source == 'BrandStoreServiceFacade/CreateAsset'});
console.log(searchV1);
console.log(searchV1.args[0]);

// or target the args element directly
let searchV2 = response.auditEvents.find(event => {return event.source == 'BrandStoreServiceFacade/CreateAsset'}).args[0];
console.log(searchV2);

1 Like

Thanks again for your help, @michaelderekjones - that’s exactly what I needed. :slight_smile:

The most important thing is I didn’t realized that:

I didn’t used find(); function until now (looks interesting) but in the end I put together something like this:

pm.environment.set('asset_id', response.auditEvents.find(event => {return event.source == 'BrandStoreServiceFacade/CreateAsset'}).args[0]);