How to transform JSON body to key=value&key2=value2

Hi All,

I’m trying to transform the JSON body of the request into a string formatted as 'key1=value1&key2=value2&key3=value3'. I have a regular JavaScript line that works great:

const bodySignParams = Object.entries(params).map(([key, val]) => key + '=' + val).join('&');

where ‘params’ is a JSON object

If I use it in an online Javascript sandbox, the result is perfect. If I try this in Postman, it interprets every character and not the key/value pair, even if I do JSON.parse(params) - so if the input is
{ "ba": "sheep", "have": "wool" }
the output is
0={&1="&3=b&4=a&5=" - you get the idea.

in this case, the output should be

How can I get Postman to treat it as key/value pairs instead of a string?

This is in the pre-request script at the collection level.

Hey @zeev.eisenberg64 :wave:

Welcome to the Postman Community! :postman:

This feels more like a StackOverflow question about Javascript than something specifically to do with Postman. I was able to find a solution on SO and get it working in the pre-request script.

I added the method of extracting the request body from Postman and logged the result out to the console:

function jsonToQueryString(json) {
    return '?' + 
        Object.keys(json).map(function(key) {
            return encodeURIComponent(key) + '=' +

const resp = JSON.parse(pm.request.body.raw);


I’m sure that there are more efficient solutions and I’m not sure if this will still work on more complex request payloads but hopefully this helps.

Thanks. I was very close, but I missed a few things. I appreciate the help.

I thought about SO, but I figured this form would be better for Postman specific-questions. SO wouldn’t know about pm.request.etc and the quirks of Postman, whatever they are. Thanks again for the quick response.

BTW, is there a way to debug/step through these scripts instead of just running them and trying to guess what wasn’t correct?

In terms of debugging your scripts, you have the Postman Console available to you, where you can use console.log() statements to inspect various parts of the script while it’s being executed.

The sandbox doesn’t have the ability to attach a debugger and step through the lines of code.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.