Hello I am attempting to sort by a Published Date but get Unexpected token var

Here’s an outline with best practices for making your inquiry.

My question:
Can I get assistance on this

Details (like screenshots):

Code Snippet:
pm.test(“Verify publish date is sort from newest to old order”,function () {
var l = require(‘lodash’),
var jsonData = pm.response.json().results[0];
responseArray = JSON.parse(jsonData);
pm.test(‘Date for article published is in sorted order’, () => {
var expectedSortedOrder = l.orderBy(responseArray, [‘publishedDate’],[‘desc’]);

It seems for me that you forgot to add “;” after “require(‘lodash’)”. So it should be:

var l = require(‘lodash’);
var jsonData = pm.response.json().results[0];

You don’t really need to “include” Lodash as its built into the Postman Sandbox.

You can access it by _. and the particular Lodash function you want.

For example (using Lodash Sample)…

var testData = ["foo", "bar"];
var option = _.sample(testData);

Thanks all but now I am getting a different type of error. any assistance would be appreciated

New Code Snippet;

pm.test("Verify publish date is sort from newest to old order",function () {
var jsonData = pm.response.json().results;
responseArray = JSON.parse(jsonData);    
pm.test('Date for article published is in sorted order', () => {
var expectedSortedOrder = _.orderBy(responseArray, ['publishedDate']);



Snippet of Json response body

Get used to console logging your variables. They are important for troubleshooting.

This way you will also be able to tell which line is causing the issue.

var jsonData = pm.response.json().results;

Looking at your example response. This should already been an array of objects.

Confirm by looking at the console log or create a test for it.

pm.test("jsonData should be an array", () => {

It will be the following line that is causing the issue.

responseArray = JSON.parse(jsonData);

You’ve already parsed the data, you don’t need to do it again unless the data is embedded in a string which might require parsing. Therefore its failing the conversion as the data already contains an array of objects.

You should be able to sort directly on the “jsonData” array.