Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

Hope you will find following function useful for converting query string into json object

var queryStringToJSON = function (url) {
    if (url === '')
        return '';
    var pairs = (url || location.search).slice(1).split('&');
    var result = {};
    for (var idx in pairs) {
        var pair = pairs[idx].split('=');
        if (!!pair[0])
            result[pair[0].toLowerCase()] = decodeURIComponent(pair[1] || '');
    }
    return result;
}

Usage:

To get current windows query string

var result = queryStringToJSON() // without any parameter

To get json from custom query string:

var result = queryStringToJSON('?name=prem&age=30&HEIGHT=5.8')

output: {name:"prem", age:"30", height:"5.8"} //All keys are converted into small letters

To convert it back to url you can use jQuery param method

$.param(result)

To manipulate your query string you can simple use standard object manipulation in JavaScript and use $.param method again

result.age=35;
delete result['name']; 
share|improve this question

closed as unclear what you're asking by Bill the Lizard Jul 27 '14 at 13:14

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

3  
So what's your question? – Tomi Lammi Dec 19 '13 at 9:32
    
he just posted his useful function to help others... so no question here... – patel.milanb Dec 19 '13 at 9:33
1  
Then why did I find this in "questions" section? – Tomi Lammi Dec 19 '13 at 9:34
    
Thanks @premChandaraSingh – Mohasin Ali May 25 '14 at 12:14
    
This does not seem to be working with array's: eg: ?=name=Tom&name=Jeff – KyorCode May 27 '14 at 12:21

2 Answers 2

Working, but if you used url || location.search then you should remove if(url === '') return '' or it should be like,

var queryStringToJSON = function (url) {
    url = url || location.search;// url or location.search
    if (url === '')
        return '';// return if url and location.search not found
    // your remainig code
}
share|improve this answer
    
Understood but this is a special check. '' is special falsy value in string manipulation. So I just want a check if falsy value is empty string just return as it is. – Premchandra Singh Dec 19 '13 at 9:46
    
Then you should not use location.search in line var pairs = (url || location.search).slice(1).split('&'); – Rohan Kumar Dec 19 '13 at 9:56

Alternatively... Instead of:

for (var idx in pairs) {
    var pair = pairs[idx].split('=');
    if (!!pair[0])
        result[pair[0].toLowerCase()] = decodeURIComponent(pair[1] || '');
}

Try:

result = pairs.reduce(function(a,b) {
     var pair = b.split("=");
     a[pair[0].toLowerCase()] = decodeURIComponent(pair[1] || '');
     return a;
},{});

Works the same but uses reduce() which being a native javascript function, is probably faster.

share|improve this answer
    
You need a polyfill for reduce if you need to support IE8 or lower. Here: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… – Dave Jul 16 '14 at 16:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.