How to ignore request line if Variable from CSV is empty

Really New to Postman, trying to make creating bulk data as easy as populating csv and running request - Cannot figure out how to skip/ignore request line if Variable is blank as it being “” appears to break the iteration and the user is not created

E.g. In request ‘RoleID1’, ‘RoleID2’ and ‘RoleID3’ may not all be required as such RoleID3 for example may need to be skipped as trying to Post with RoleID3 being blank (see Screenshot of spreadsheet) results in the user entry not being created

Screenshots
Postman Request
Spreadsheet Roles

Hey welcome to postman community :hugs:Please add the expected request structure when a field is blank

Not sure what the expected structure would be (really new so not surebut if you can advise where I would find this I can try get it), just need it to skip/ignore the role line(s) if the variable is blank/null so that the user is created with all other applicable roles and the blank/null ones dont block creation

could you add the request body as string here than image

Ah sorry here it is:

{

    "type": "{{UserType}}",

    "password": "{{UserPassword}}",

    "name": "{{UserName}}",

    "username": "{{UserUserName}}",

    "email": "{{UserEmail}}",

    "status": {{UserStatus}},

    "_links": {

            "role":[

                {"href": "/roles/{{RoleID1}}"},

                {"href": "/roles/{{RoleID2}}"},

                {"href": "/roles/{{RoleID3}}"}

            ]

        }

}

so if RoleID3 is empty is this what you expect?

{

    "type": "{{UserType}}",

    "password": "{{UserPassword}}",

    "name": "{{UserName}}",

    "username": "{{UserUserName}}",

    "email": "{{UserEmail}}",

    "status": {{UserStatus}},

    "_links": {

            "role":[

                {"href": "/roles/{{RoleID1}}"},

                {"href": "/roles/{{RoleID2}}"}



            ]

        }

}

Yup, same for if RoleID1 or RoleID2 are empty etc. all other fields will always be populated so just need the RoleID rows to be removed/ignored if the variable in the row is blank/null

1 Like

Add this to your pre request script :

let roleList = []

data["RoleID1"] !== "" ? roleList.push({ "href": "/roles/{{RoleID1}}" }) : null

data["RoleID2"] !== "" ? roleList.push({ "href": "/roles/{{RoleID2}}" }) : null

data["RoleID3"] !== "" ? roleList.push({ "href": "/roles/{{RoleID3}}" }) : null

pm.variables.set("roleList", JSON.stringify(roleList))

Now change the request body as :

   {

    "type": "{{UserType}}",

    "password": "{{UserPassword}}",

    "name": "{{UserName}}",

    "username": "{{UserUserName}}",

    "email": "{{UserEmail}}",

    "status": {{UserStatus}},

    "_links": {

            "role":{{roleList}}

        }

}

we are using an ternary operator to decide whether to add that to list or not , its cleaner than if statement.

then converting it to string as request body is feed us string and is parsed in backend

3 Likes

Looks like that worked perfectly thank you!, I assume to expand that for additional role entries its as simple as expanding the pre-request script with an additional line for say RoleID4? like so:

data[“RoleID4”] !== “” ? roleList.push({ “href”: “/roles/{{RoleID4}}” }) : null

Building onto this further, what would the simplest way to have it ignore “role” entirely if all RoleIDs are blank is that possible using what we have?

1 Like

Try something on the same logic , check if roleList length is 0 , then do something like that

So I changed body to

{
    "type": "{{UserType}}",
    "password": "{{UserPassword}}",
    "name": "{{UserName}}",
    "username": "{{UserUserName}}",
    "email": "{{UserEmail}}",
    "status": {{UserStatus}},
    "_links": {
            {{role}}:{{roleList}}
        }
}

and Pre-request to

let roleList = []

data["RoleID1"] !== "" ? roleList.push({ "href": "/roles/{{RoleID1}}" }) : null

data["RoleID2"] !== "" ? roleList.push({ "href": "/roles/{{RoleID2}}" }) : null

data["RoleID3"] !== "" ? roleList.push({ "href": "/roles/{{RoleID3}}" }) : null

pm.variables.set("roleList", JSON.stringify(roleList))

let role = []

roleList != "" ? role.push("role") : null

pm.variables.set("role", JSON.stringify(role))

However the request is then sending as the following if there is role data

["role"]:[{"href":"/roles/1"},{"href":"/roles/2"},{"href":"/roles/75"}]

and the below if there is not

[]:[]

Its not creating users now though which I believe is down to it being [“role”]: rather than “role”: but im unsure how to remove the square brackets ?

almost there :

let roleList = []

data["RoleID1"] !== "" && data["RoleID1"] !== undefined && data["RoleID1"] !== null ? roleList.push({ "href": "/roles/{{RoleID1}}" }) : null
data["RoleID2"] !== "" && data["RoleID2"] !== undefined && data["RoleID2"] !== null ? roleList.push({ "href": "/roles/{{RoleID2}}" }) : null
data["RoleID3"] !== "" && data["RoleID3"] !== undefined && data["RoleID3"] !== null ? roleList.push({ "href": "/roles/{{RoleID3}}" }) : null


roleList.length? pm.variables.set("role", JSON.stringify({ "role": roleList })):pm.variables.set("role", JSON.stringify({  }))

Body:

{

    "type": "{{UserType}}",

    "password": "{{UserPassword}}",

    "name": "{{UserName}}",

    "username": "{{UserUserName}}",

    "email": "{{UserEmail}}",

    "status": {{UserStatus}},

    "_links": {{role}}

}
4 Likes

Ah awesome, so to make sure I understand what its doing there, prerequest is setting rolelist to empty - its then checking if the data from the sheet is NOT empty, NOT undefined, NOT null and then pushing the string of ‘{ “href”: “/roles/{{RoleID1}}” }’ etc into the rolelist variable otherwise its nulling it if it fits any of the above checks (empty / null etc )

Then the bottom line is checking if roleList has any length, if it does its then setting the string of role variable to “role”:“roleList String” where as if it has no length in roleList its setting it to { }

Is that right?

1 Like

That’s Correct :hugs:. Request body is string and postman does the parsing in backend so we defines what ever json structure we want and stringifies it

1 Like