In a previous post I wrote about using momentjs within Postman, this is an excellent utility module that makes working with times and dates in JavaScript so much easier.

I wanted to continue my love for these utility modules by introducing another one that can be used within the application. This time it’s Lodash, this just makes working with arrays, objects, numbers, strings etc. So much easier by providing a library of amazing methods that take some of the pain away from using native JavaScript.

I’m not going to go too in-depth with explaining the whole module because it’s epic and this post could go on forever but I wanted to give you some basic examples, using a few of the functions, for certain use cases within Postman. My aim is to get you interested in using Lodash and give you a starting point from which to explore some of the other functions that the library has to offer.

Accessing the Lodash module within the application

There is for some reason (I have no idea why…) two different ways to get access to Lodash, for all the version 4.17.4 functions you will need to add a `require(‘lodash’)` statement at the start of your scripts.

The way that I would use it is slightly different though, most of the features that I have ever used are contained in version 3.10.1 and to use these you don’t need to ‘require’ the module, you just simply jump straight in and use the `_` variable in your scripts.

So that’s how to get access to the module but how do we use it and why is it better easier than using native JavaScript?

Some very basic usage examples

The `Pre-request Script` and `Tests` tabs are basically little JavaScript IDE’s and they give us the ability to write small scripts, to assist us with things like creating test data or managing the response data that we receive from an API endpoint.

In my limited experience, a whole group of questions that I’ve answered on Stackoverflow have been related to checking specific parts of a JSON response – This is where I feel Lodash makes life easier, the native JS syntax for looping through data looks awful and if you’re not too familiar with the language, it’s easy to get it wrong and be left confused.

Using the native JavaScript `for` loop just looks ugly and I’ve lost count of the number of times that I have googled the correct syntax to use! 😦 This first example is using the built-in `pm.response.json()` function which parses the JSON response body data.

This is the native JS `for` loop that would iterate through the data and build up an index of the items using the .length method to know when it’s done.

Native JS ‘for’ loop


This is the Lodash for loop using the .each() function, using `forEach()` is the same thing but I’m using its alias instead. This would iterate through the same data and hold all the data in the item index, ready to be used.

Lodash ‘for’ loop


Let’s take a look at this in action with Postman. I’m using the loop with the pm.test() function, to check that the gender property of each object in the array, is either Male or Female. The pm.expect() is using the Chaijs syntax.

Using the _.each() loop in Postman


Another example of where the Lodash functions can help us out, is when we need to create some dynamic data to use in our requests. In the Pre-request Script tab, we can create all of this data before the main request is sent.

There are a couple of handy dynamic variables in Postman like ‘{{$randomInt}}‘ that help with placing a random number into a request but these are quite limited in terms of the number range. Lodash has the ‘_.random()‘ function that extends this slightly and will give a bit more control over the range of numbers returned.


The ‘_.random()’ Lodash function syntax


Using these in Postman is very simple, all we need to do is use either the environment or globals postman set function, to set the variable value. This can then be used in our requests with the `{{var_name}}` syntax.

Using the ‘_.random()’ function in Postman


Very briefly we have seen how useful Lodash can be when creating data for our requests or easily iterating through some response data, to help assert that a certain value is present. Postman is great for just debugging responses too, not every request you build needs to have a Test or used as part of an automated check suite.

I love using the Postman Console, this allows me to inspect different parts of the request or the response, to narrow down problems that I may be having. Pretty much every thing that you do in Postman can be logged out to the console using a simple `console.log()` statement in either the Pre-request Scripts or Tests tab.

I’m going to look at possible (totally made up…) use case where we can use the _.chain() function to group multiple Lodash functions, in order to filter down the response data, to log out a specific area that is causing us a problem. The sample JSON data below is just a group of users that are returned over our mock API endpoint.

Sample JSON response data


The _.chain() function allows us to use lots of different functions, at the same time, to change and manipulate the response data. This can help us focus in on the parts of the data that are important to us, when looking at our problem.

There is a kinda book-end feel to this function, we start it with the _.chain() function and close it out with the .value() function. The part in the middle is doing the leg work and each one is changing the data, as it passes through, leaving us with the value(s) that we want to display.

Basically, I’m using the .map() function to get all the values from the ‘favourite_colour‘ property and placing those into separate arrays, then I’m using the .flatten() function to flatten the arrays into a single array. Finally, I’m using the _.uniq() function to filter the flattened array, to only return unique ‘favourite_colour’ values.

Lodash ‘_.chain()’ function


There is a lot going on there but hopefully it’s short enough to follow the data path. This is the console output of the filtered down response data, showing us all the unique ‘favourite colours’, from all of our users.

Using the _.chain() function in Postman


So that was a very quick and basic look and using Lodash and it’s many wonderful functions within the Postman application. The module is huge and I would totally recommend exploring it to find the functions that could be useful in your context.

Here are some additional Lodash resources that might be useful to you, they all contain examples of ways that some of the functions can be used: