Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
It converts string like "parents[child[bla]]" into json
function convertInputStringToJson(inputName){
var r = inputName.replace(/]/g, '').split('[');
var v = {};
var lastEl = null;
for(var i=0; i<r.length; i++) {
if(!Object.keys(v).length){
v[r[i]] = {};
lastEl = v[r[i]];
} else {
lastEl[r[i]] = {};
lastEl = lastEl[r[i]]
}
}
return v;
}
/* convertInputStringToJson('parents[child[bla]]')
result: {
"parents": {
"child": {
"bla": {}
}
}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.