Tried to find how to make {foo:"bar"}
from ?...&foo=bar&...
but googled and got only to jQuery.params
which does the opposite. Any suggestions please (built-in javascript function, jQuery, Underscore.js - all goes)? Or, do I need to implement it by myself (not a big hassle, just trying not to reinvent the wheel)?

- 15,447
- 5
- 79
- 98

- 10,940
- 26
- 72
- 108
-
2well here you go: http://stackoverflow.com/questions/901115/get-query-string-values-in-javascript . A little search always helps. – ggozad Jul 19 '12 at 09:27
-
@ggozad: searched for a *conversion into JSON/object*, not just *getting values*. thanks for the link! sadly this function isn't standard somewhere in jQuery or so. – BreakPhreak Jul 19 '12 at 09:30
7 Answers
Actually the above answer by @talsibony doesn't take into account query string arrays (such as test=1&test=2&test=3&check=wow&such=doge
). This is my implementation:
function queryStringToJSON(qs) {
qs = qs || location.search.slice(1);
var pairs = qs.split('&');
var result = {};
pairs.forEach(function(p) {
var pair = p.split('=');
var key = pair[0];
var value = decodeURIComponent(pair[1] || '');
if( result[key] ) {
if( Object.prototype.toString.call( result[key] ) === '[object Array]' ) {
result[key].push( value );
} else {
result[key] = [ result[key], value ];
}
} else {
result[key] = value;
}
});
return JSON.parse(JSON.stringify(result));
};

- 442
- 7
- 15
-
1Great answer, thanks. I love that it creates arrays out of multiple select checkboxes. – GTS Joe Dec 31 '19 at 15:31
-
Good answer but i think its better with if (key !== "") on top of your if statements – mortezashojaei Nov 14 '20 at 14:10
-
1If & is there in value then it is not working. Any solution for it? test=1&test=2&2&test=3&check=wow&such=doge – Vish V Feb 22 '21 at 12:32
-
What is the situation where there is an actual ampersand in a query string? Won't any ampersands in a URL be encoded as `%26`? – Raphael Aug 11 '22 at 02:48
-
1
I am posting here my function just in case other will look and will want to get it straight forward no need for jquery native JS. Because I was looking for the same thing and finally made this function after viewing others answers:
function queryStringToJSON(queryString) {
if(queryString.indexOf('?') > -1){
queryString = queryString.split('?')[1];
}
var pairs = queryString.split('&');
var result = {};
pairs.forEach(function(pair) {
pair = pair.split('=');
result[pair[0]] = decodeURIComponent(pair[1] || '');
});
return result;
}
console.log(queryStringToJSON(window.location.href));
console.log(queryStringToJSON('test=1&check=wow'));//Object {test: "1", check: "wow"}

- 8,448
- 6
- 47
- 46
-
1If & is there in value then it is not working. Any solution for it? test=1&test=2&2&test=3&check=wow&such=doge – Vish V Feb 22 '21 at 12:32
-
1if its there I think it needs to be url encoded like below: queryStringToJSON('test6=1&test3=2%262&test1=3&check=wow&such=doge') – talsibony Feb 23 '21 at 13:00
You have Ben Alman's jQuery BBQ
and a jQuery.deparam
in it. It is described as The opposite of jQuery.param, pretty much.
http://benalman.com/code/projects/jquery-bbq/examples/deparam/
First example is exactly what you need.

- 1,953
- 12
- 14
In modern browsers, you can also use Object.fromEntries which makes this even easier.
function queryStringToObject(queryString) {
const pairs = queryString.substring(1).split('&');
// → ["foo=bar", "baz=buzz"]
var array = pairs.map((el) => {
const parts = el.split('=');
return parts;
});
// → [["foo", "bar"], ["baz", "buzz"]]
return Object.fromEntries(array);
// → { "foo": "bar", "baz": "buzz" }
}
console.log(queryStringToObject('?foo=bar&baz=buzz'));
The URLSearchParams interface can Interactive with the browsers URL search parameters. The browser support for URLSearchParams is pretty decent.
For your case, it would be:
console.log(
Object.fromEntries(new URLSearchParams('foo=bar&baz=buzz'))
);

- 15,447
- 5
- 79
- 98
for simple and flat query strings, something like this will do the trick
const queryStringToObject = (queryString) => {
let obj = {}
if(queryString) {
queryString.slice(1).split('&').map((item) => {
const [ k, v ] = item.split('=')
v ? obj[k] = v : null
})
}
return obj
}
> queryStringToObject('?foo=bar&baz=buzz')
{ foo: 'bar', baz: 'buzz' }

- 33,652
- 11
- 120
- 99
-
It deletes the first character for query strings like `data=thisisatestdata` – Amirition Oct 30 '19 at 16:32
-
you'd want to remove the call to `slice` if you are including the question mark, or consider extending the method to support both string types – random-forest-cat Oct 30 '19 at 17:20
-
also if there are empty values in query strings then it just removes the key from the object, it should be empty strings instead. – Sunil Lulla Apr 11 '20 at 13:32
The URLSearchParams()
constructor creates and returns a new URLSearchParams object.
var url = new URL('https://example.com?foo=1&bar=2');
var params = new URLSearchParams(url.search);
https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/URLSearchParams

- 863
- 1
- 10
- 22
I know this thread is a bit old - but this is what worked for me - hopefully it will help someone else too ..
var _searchModel = yourquerystring.split("&");
for (var x = 0; x < _searchModel.length; x++) {
//break each set into key and value pair
var _kv = _searchModel[x].split("=");
//console.log(_kv);
var _fieldID = _kv[0];
var _fieldVal = _kv[1];
}

- 79
- 1
- 3
-
Snippet now working as expected, it has error `ReferenceError: yourquerystring is not defined` – dipenparmar12 Feb 23 '21 at 11:51