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

**URL:** <https://community.postman.com/t/how-to-transform-json-body-to-key-value-key2-value2/57827>\
**Category:** Help Hub\
**Tags:** scripts\
**Created:** [February 19, 2024, 6:35pm UTC](https://community.postman.com/t/how-to-transform-json-body-to-key-value-key2-value2/57827 "2024-02-19T18:35:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zeev.eisenberg64](https://avatars.discourse-cdn.com/v4/letter/z/a88e57/32.png) [@zeev.eisenberg64](https://community.postman.com/u/zeev.eisenberg64)\
**Post date:** [February 19, 2024, 6:35pm UTC](https://community.postman.com/t/how-to-transform-json-body-to-key-value-key2-value2/57827/1 "2024-02-19T18:35:57Z")

</div>

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:

```auto
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  
`ba=sheep&have=wool`

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.

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [February 20, 2024, 10:15am UTC](https://community.postman.com/t/how-to-transform-json-body-to-key-value-key2-value2/57827/2 "2024-02-20T10:15:01Z")

</div>

Hey @zeev.eisenberg64 👋

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](https://stackoverflow.com/a/30707423/6028443) on SO and get it working in the pre-request script.

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/5/c/5c426d4234b623300eb4f9a5909a8d4c7f8ab611.png)

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

```auto
function jsonToQueryString(json) {
    return '?' + 
        Object.keys(json).map(function(key) {
            return encodeURIComponent(key) + '=' +
                encodeURIComponent(json[key]);
        }).join('&');
}

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

console.log(jsonToQueryString(resp));

```

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.

---

<div class="post-metadata">

**Author:** ![zeev.eisenberg64](https://avatars.discourse-cdn.com/v4/letter/z/a88e57/32.png) [@zeev.eisenberg64](https://community.postman.com/u/zeev.eisenberg64)\
**Post date:** [February 20, 2024, 7:16pm UTC](https://community.postman.com/t/how-to-transform-json-body-to-key-value-key2-value2/57827/3 "2024-02-20T19:16:48Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [February 21, 2024, 10:58am UTC](https://community.postman.com/t/how-to-transform-json-body-to-key-value-key2-value2/57827/4 "2024-02-21T10:58:07Z")

</div>

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.

> **[Debug API requests using the Postman Console | Postman Learning Center](https://learning.postman.com/docs/sending-requests/troubleshooting-api-requests/)**
>
> Debug API requests using the Postman Console: documentation for Postman, the collaboration platform for API development. Create better APIs—faster.

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

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [March 22, 2024, 10:58am UTC](https://community.postman.com/t/how-to-transform-json-body-to-key-value-key2-value2/57827/5 "2024-03-22T10:58:59Z")

</div>

Closed on March 22, 2024, 10:58am UTC

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