Unable read complex sorting object from request body and validate whether the response is in the defined sorting order

I am beginner in Postman. I have the following Post request body
“pageNumber”: 1,
“pageSize”: 10,
“sortParams”: [
“sortColumn”: “BranchCode”,
“sortOrder”: “Asc”
“filterParams”: {
“filterColumn”: “BranchCode”,
“FilterValue”: “MAA”,
“operator”: “eq”,
“logicOperator”: “and”,
“isCaseSensitive”: false,
“filterList”: null

The response is as follows

"pageNumber": 1,

"pageSize": 10,

"totalPages": 1,

"totalRecordsCount": 1,

"succeeded": true,

"message": null,

"errors": null,

"data": [


        "branchId": 449,

        "branchCode": "MAA",

        "branchName": "TEST Chennai",

        "branchCodeWithName": "MAA - TEST Chennai",

        "companyId": 19,

        "companyCode": "GIN",

        "companyCompanyName": null,

        "companyCompanyCodeWithName": null

    }  ]}

I am trying to get the request body and parse it and trying to validate it with reponse.
I wrote the following statements

let reqbody = pm.request.body

console.log(typeof reqbody) //returns object

let reqbody1 = pm.request.body.raw

console.log(typeof reqbody1) //returns string

when I try to parse either reqbody or reqbody1, I get the following error

JSONError: Unexpected token ’ ’ at 2:1"pageNumber": 1,

I tried replacing /trimming the spaces by using
myjson = reqbody.replace(/^\s+|\s+$/g, “”);
It says its not a function(type error)

Also pm.request.body.raw.sortParams returns undefined

I am really stuck as to what to do further. Can anyone please help me on how to get the request body or reading the complex sorting obj from request? Is this the right way of writing the script for this paticular usecase? Is there any other way to read the sorting obj from request and validate the sorting order of response?

Hey @saisubhashini :wave:

Thanks for reaching out and sharing the details :pray:

I tried to replicate the error you are seeing but it worked fine for me. Please see below for what I did:

  1. I pasted your request body into a sample request

  2. In the Tests tab, I use the following script:

  3. This is the returned value from console.log(reqBody.sortParams)
    Screen Shot 2021-04-13 at 11.54.25 am

Hope this helps!

Hi @taehoshino ,
Thank you for replying. I tried what you have shown in the screenshot too. But still getting the same error. Please see the following

Not sure what the problem exactly is. Can u please suggest me any other way to do this? Or Can u suggest me how to validate that the response is in the specified sorted order? Kindly help me out with this?

In your screenshot you have tried it with GET method. My usecase uses POST method for this. When you try with POST method with the same request body am getting JSON error on trying to read the request bosy. Waiting for your reply for my query…Kindly check…Awaiting your reply

Hey @saisubhashini :wave:

I tried with POST request but there was no issue.

From looking at the error “JSONError : Unexpected Token”, there appears to be some issues with the JSON body.
Try out with a very simple example such as

 "test": "this is a test"      

and check the console.log output to see if JSON.parse(pm.request.body.raw) is correctly obtained :slightly_smiling_face:

Thanks for replying. I tried with the example you have shared.
I wrote


Console output

Its working fine…But, for my usecase am still facing the same issue. When I try to parse the request body, I get error and when I try to access sortParams within the request body without parsing it says undefined…Unable to resolve the issue…Can u kindly show a way? Just want to know why this issue is occurring…

Hey @saisubhashini :wave:

Sorry to hear that you are still seeing the error.

If possible, can reproduce the issue using Postman Echo API and then share the collection file so that I can try reproducing on my end using the same request?

Thanks for replying. I have tried resolving the issue. Its an indentation issue. I think there are few invisible characters. JSON body in a particular format was only being accepted. I have made the required changes. Its working now. Thanks for the support.

1 Like

Glad to hear that the issue was resolved now!! :tada: