# Convert a flat JSON object to x-www-form-urlencoded

**URL:** <https://community.smartbear.com/t/convert-a-flat-json-object-to-x-www-form-urlencoded/59>\
**Category:** API Articles\
**Tags:** readyapi, api, migrated, kb-article\
**Created:** [September 28, 2021, 12:08pm UTC](https://community.smartbear.com/t/convert-a-flat-json-object-to-x-www-form-urlencoded/59 "2021-09-28T12:08:28Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@system](https://community.smartbear.com/u/system)\
**Post date:** [September 28, 2021, 12:08pm UTC](https://community.smartbear.com/t/convert-a-flat-json-object-to-x-www-form-urlencoded/59/1 "2021-09-28T12:08:28Z")

</div>

> Originally published by **aaronpliu** on September 28, 2021 in the previous SmartBear Community.

**Question**

How to create a Groovy script, which transforms a non-nested JSON object to the x-www-form-urlencoded format before sending it in the request body.

Example: converting this object:

```auto
{
  "name": "John",
  "age": 30,
  "city": "New York"
}

```

should result in this string:

```auto
name=John&age=30&city=New%20York 

```

**Answer**

```auto
def jsonData = '''
{
	"name":"John",
	"age":30,
	"city":"New York"	
}
'''
def slurper = new groovy.json.JsonSlurper().parseText(jsonData)
def str = new StringBuilder()
def iter = slurper.keySet().iterator()
while(iter.hasNext()){
	def key = iter.next().toString()
	def value = slurper.get(key).toString().trim().replaceAll(" ","20%")
	str.append("$key=$value&")
}
log.info str[0..str.size()-2]

```
