Skip to content

Instantly share code, notes, and snippets.

@colelawrence
Last active August 29, 2015 14:03
Show Gist options
  • Save colelawrence/590940dcb906cbb27eb3 to your computer and use it in GitHub Desktop.
Save colelawrence/590940dcb906cbb27eb3 to your computer and use it in GitHub Desktop.
Convert json to form formatted JSON
process = (str, obj) ->
res = {}
bl = (cont_str, cobj) ->
if typeof cobj is 'object'
for k, v of cobj
bl cont_str + "[#{ k }]", v
else
res[cont_str] = cobj
bl str, obj
return res
o = {
h:
j: true
g: 2
m:
w: true
m: false
s: true
}
Say JSON.stringify(process("child",o),null,2)
# {
# "child[h][j]": true,
# "child[h][g]": 2,
# "child[h][m][w]": true,
# "child[h][m][m]": false,
# "child[s]": true
# }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment