Serialize object to query string in JavaScript/jQuery [duplicate]

I'm trying to find information on how to serialize an object to query string format, but all my searches are drowning in results on how to go the other way (string/form/whatever to JSON).

I have

{ one: 'first', two: 'second' }

and I want


Is there a good way to do this? I don't mind plugins or whatnots - if the code I find is not a plugin, I'll probably re-write it to one anyway...



You want $.param():

Specifically, you want this:

var data = { one: 'first', two: 'second' };
var result = $.param(data);

When given something like this:

{a: 1, b : 23, c : "[email protected]#st"}

$.param will return this:


For a quick non-JQuery function...

function jsonToQueryString(json) {
    return '?' + 
        Object.keys(json).map(function(key) {
            return encodeURIComponent(key) + '=' +

Note this doesn't handle arrays or nested objects.


Alternatively YUI has

For example:

var data = { one: 'first', two: 'second' };
var result = Y.QueryString.stringify(data);

Another option might be node-querystring.

It's available in both npm and bower, which is why I have been using it.


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.