I am using jQuery Serialize to serialize my form elements and would like to deserialize them back. Unfortunately can't find any working jQuery deserializer, any suggestions?
-
You need to provide more information. Do you want to deserialize them on the server? In which, case it would be helpful if you told us what you are using? Rails, PHP, ASP.net, Java? – Jaryl Aug 09 '11 at 07:48
-
1Sorry, I forgot to add that I want to deserialize data on client side using JS. – Tomas Aug 09 '11 at 08:18
11 Answers
I wrote a version of jQuery.deserialize that supports serialized data generated from the serialize, serializeArray and serializeObject functions. It also supports all form element types, including checkboxes and radio buttons.

- 2,187
- 2
- 16
- 15
-
8
-
-
Hey man. Awesome plugin I just wanted to know that does your plugin provides a way that I could deserialize a serialized array and store it in a variable – Gardezi Nov 25 '16 at 16:22
-
Try this:
function deparam(query) {
var pairs, i, keyValuePair, key, value, map = {};
// remove leading question mark if its there
if (query.slice(0, 1) === '?') {
query = query.slice(1);
}
if (query !== '') {
pairs = query.split('&');
for (i = 0; i < pairs.length; i += 1) {
keyValuePair = pairs[i].split('=');
key = decodeURIComponent(keyValuePair[0]);
value = (keyValuePair.length > 1) ? decodeURIComponent(keyValuePair[1]) : undefined;
map[key] = value;
}
}
return map;
}

- 14,554
- 11
- 45
- 57
I was very interested in trying JQuery.deserialize, but it didn't seem to handle checkboxes at all, so it didn't serve my purposes. So I wrote my own. It turned out to be easier than I thought, because the jQuery val() function does most of the work:
jQuery.fn.deserialize = function (data) {
var f = this,
map = {},
find = function (selector) { return f.is("form") ? f.find(selector) : f.filter(selector); };
//Get map of values
jQuery.each(data.split("&"), function () {
var nv = this.split("="),
n = decodeURIComponent(nv[0]),
v = nv.length > 1 ? decodeURIComponent(nv[1]) : null;
if (!(n in map)) {
map[n] = [];
}
map[n].push(v);
})
//Set values for all form elements in the data
jQuery.each(map, function (n, v) {
find("[name='" + n + "']").val(v);
})
//Clear all form elements not in form data
find("input:text,select,textarea").each(function () {
if (!(jQuery(this).attr("name") in map)) {
jQuery(this).val("");
}
})
find("input:checkbox:checked,input:radio:checked").each(function () {
if (!(jQuery(this).attr("name") in map)) {
this.checked = false;
}
})
return this;
};
You should be able to use this like this:
$("#myform").deserialize(data);
Where data is a parameter list such as what $("#myform").serialize() would produce.
It affects all fields in the form, and it will clear the values of fields that are not contained in the data. But you can also pass any selector to affect only specific fields, as you can with the serialize function. E.g.:
$("select").deserialize(data);

- 8,041
- 1
- 47
- 42

- 3,286
- 1
- 24
- 18
-
function `decodeURIComponent()` somehow fails at translating `+` (plus) to ` ` (space). Had to add some regular expression to corretly handle plus/space. – David162795 Nov 18 '15 at 17:54
-
@David162795, that's true. I had worked around this myself when I used this function by doing this: `params.replace(/\+/g, "%20")` I never added this to the function because I wasn't 100% sure it worked in all situations (though I never thought of a situation where it would be a problem). Is that the same regular expression you used? – David Hammond Nov 19 '15 at 18:35
-
This funciton deserializes into all elements in document, resetting all checkboxes and fields with matching names. And if you deserialize empty string it would also reset values of all inputs in page with empty name. – TiGR Jul 25 '16 at 12:00
-
@TiGR, the intention is to do the opposite function of the serialize function. If you do $("#myform").deserialize(data), it affects all fields in the form (not all fields in the document, as you state). But you can also pass any selector to affect only specific fields. I just updated the answer to clarify that a bit better. – David Hammond Jul 25 '16 at 13:21
Half of jQuery Serialize is param(), so half of something that deserializes a query string is going to be a deparam. Unfortunately I haven't been able to find a good standalone deparam. For now I recommend getting the jQuery BBQ library and using that. If you don't need the other stuff you can remove them. I read somewhere that Ben Alman (cowboy) planned to extract deparam into its own module.
For the rest of deserializing, you'll just need to loop through the object that deparam returns and for each key and value pair in the object, select the form element based on the key, and set the form elements value to the value.

- 14,071
- 7
- 61
- 60
Bit late on this one, but somebody might find this useful.
function fetchInput(identifier) {
var form_data = identifier.serialize().split('&');
var input = {};
$.each(form_data, function(key, value) {
var data = value.split('=');
input[data[0]] = decodeURIComponent(data[1]);
});
return input;
}

- 5,699
- 6
- 38
- 63

- 83
- 2
- 6
-
This works well enough for single-dimension arrays; I would change `input[decodeURIComponent(data[0])] = ...` so that key names like `users[]` and `address[billing]` get properly decoded too. – Jared Farrish Apr 19 '17 at 16:14
-
Although I'm just now noticing it doesn't play well with dynamic fields like `customers[]` where you may have more than one that generate an array when consumed by the server. – Jared Farrish Apr 19 '17 at 16:26
I'm not now answering your question but my guess is that you want to serialize it and send back to server and then use the serialized data which is why you have to deserialize it?
If that's the case you should consider using .serializeArray(). You can send it as POST data in ajax, and then access later as well because you will have object.
Using Jack Allan's deparam function with jQuery, you can change this line:
map[key] = value;
to
$('input[name=' + key + ']').val(value);
Which will load the data back into your form fields.

- 2,907
- 1
- 25
- 16
this code returns an array when same key is spotted multiple times in the serialized string
chaine="single=Single1&multiple=Multiple&multiple=Multiple1&multiple=Multiple2&multiple=Multiple3&check=check2&radio=radio1"
function deserialize(txt){
myjson={}
tabparams=chaine.split('&')
var i=-1;
while (tabparams[++i]){
tabitems=tabparams[i].split('=')
if( myjson[decodeURIComponent(tabitems[0])] !== undefined ){
if( myjson[decodeURIComponent(tabitems[0])] instanceof Array ){
myjson[decodeURIComponent(tabitems[0])].push(decodeURIComponent(tabitems[1]))
}
else{
myjson[decodeURIComponent(tabitems[0])]= [myjson[decodeURIComponent(tabitems[0])],decodeURIComponent(tabitems[1])]
}
}
else{
myjson[decodeURIComponent(tabitems[0])]=decodeURIComponent(tabitems[1]);
}
}
return myjson;
}

- 11
- 1
Needed all in a single string, which can be stored in maybe COOKIE, and later read and fill the same form with input values.
Input elements seperator: ~ (use any seperator)
Input attributes seperator: | (use any seperator)
input type | input name | input value ~ input2 type | input2 name | input2 value
var formData = '';
$('#form_id').find('input, textarea, select').each(function(i, field) {
formData += field.type+'|'+field.name+'|'+field.value+'~';
});
Example:
hidden|vote_id|10~radio|option_id|~radio|option_id|427~radio|option_id|428~

- 1,364
- 15
- 23
If what you want is to remove the standard URL-encoded notation, you can use JavaScript's decodeURIComponent(), which will give you a regular string, just like this:
var decodedString = decodeURIComponent("Http%3A%2F%2FHello%3AWorld");
alert(decodedString);
In this case, decodedString will look like Http://Hello:World
, here's a working fiddle.
Got all of this searching for this same issue, and found the answer here: How can I decode a URL with jQuery?
I know this is an old question, but doing some searches for jQuery deserialize got me here, so I might as well try to give a different approach on the issue for people with the same problem.

- 1
- 1

- 2,815
- 1
- 16
- 20
May be a bit late, but perhaps you are looking for something like JQuery.deserialize. Problems: no support for checkboxes or radio buttons.

- 14,692
- 17
- 59
- 96