I have use case where we are getting nested key value in x-www-form-urlencoded body like below
name=abc&age=12¬es[key1]=value1¬es[key2]=value2
I tried url.ParseQuery("name=abc&age=12¬es\[key1\]=value1¬es\[key2\]=value2") but it's giving
{
"name": "abc",
"age": 12,
"notes[key1]": "value1",
"notes[key2]": "value2"
}
How can I get this value in below JSON format for above body
{
"name": "abc",
"age": 12,
"notes": {
"key1": "value1",
"key2": "value2"
}
}
There is possibility that notes might be in 3 level nested format
I have tried url.ParseQuery and r.Form but both of them giving notes[key1] and notes[key2].
To unmarshal nested key values with this type of query string parameter names, it's possible to use
derekstavis/go-qswhich is a port of Rack's query string parser.This returns a
map[string]interface{}with nested key values as follows.A complete example is available on the Go Playground with the following code:
Go Playground URL: https://go.dev/play/p/Px7uZWRNs5V