Query string convert to json

Hi everyone,

I was wondering if anyone has managed to convert a query string to json format.

example from name=john&age=22&class=mca to {“name”:“john”,“age”:“22”,“class”:“mca”}

I found something similar when researching but was never able to make it work see below
function jsonToQueryString(json) {
return ‘?’ +
Object.keys(json).map(function(key) {
return encodeURIComponent(key) + ‘=’ +
encodeURIComponent(json[key]);
}).join(’&’);
}

The idea of my test scenario is the follow

  1. get request
  2. verify HTTPS
  3. convert into JSON format
    4.using JsonSlurper/slurper.parseText to store any of the values example name
  4. make name to global variable for next test scenario

I have to convert it to JSON format in order to make the JsonSlurper working to store the values.

Thanks
Boris

I’m not sure I’m following you correctly.

It seems you want to start with a querystring and convert it to JSON.

Then you show a JavaScript function which takes JSON and converts it to a querystring (the opposite of what you seem to be asking for).

Then you talk about JsonSlurper which takes JSON and produces something directly usable in Groovy/Java (maps, arrays/lists etc) - and again, almost the opposite of what you seem to be asking for.

It makes me wonder if you are looking for groovy.json.JsonOutput

http://docs.groovy-lang.org/latest/html/gapi/groovy/json/JsonOutput.html

1 Like

Thank you very much, I believe this would work per your suggestion.