Monday, July 27, 2015

advanced golang json

This might not actually be advanced but it is something that was bothering me about marshaling json that was really bothering. The alternate article title might have been better.

"how to convert a query string to json?"

A legacy system I'm working with uses query strings to store and communicate data. There are no real complaints about that structure as it served it's purpose as a universal container which is easily parsed. But as I want to unmarshal that data into a golang structure there had to be a better way.

My first thought was to strings.Split(s,"&") and again strings.Split(s[i], "="). While it works for my data it is less than perfect. The first challenge is encoding. The second is all the stupid rules for embedding special chars (probably also encoding)

I fixed that problem by using url.ParseQuery().

Now that I had a map[string][]string I had to iterate over a while loop and store the data into the structs fields. The first attempt was to use reflection. That failed because each attribute has a slightly different case and that boxed me in. But then I decided to convert map[string][]string to map[string]string and that was pretty simple.  Once in that format it is pretty simple to Marshal that array to a string and then back, again, to the actual structure.

qvalues, err := url.ParseQuery(p.origDataWrapper.OrigData)
if err == nil {
    vals := make(map[string]string)
    for k, v := range qvalues {
        if len(v) > 0 {
            vals[k] = v[0]
    buf, _ := json.Marshal(vals)
    p.err = json.Unmarshal([]byte(buf), p.origData)
. . .

There might be a way to improve that first-forloop

No comments:

Post a Comment

Fire OS and Android

The relationship between Amazon's FireOS and Google's Android OS is probably very complicated. And while there is probably some very...