Decoding gZip json

I posted this question on stack overflow, but I’m not having many responses.

The one I did get confused me further :confused:. The docs state that I need reader, err = gzip.NewReader(resp.Body) , see…

A [gunzip] Reader is an io.Reader that can be read to retrieve uncompressed data from a gzip-format compressed file.

And then with the new uncompressed reader I can pass that into the decoder, as per json.NewDecoder(reader).Decode(&response).

Firstly, is my understanding here correct. Secondly whats not working correctly in my code?

I guess thats a double whammy question, but thanks in advanced.

That looks correct enough to me. For the second question I can’t say as you don’t explain what happens or show the complete code.

@calmh to the rescue!

The full code (minus the api key) is available here:
https://play.golang.org/p/4eCuXxXm3T

Looks reasonable enough to me, but we still don’t know what happens other than that it’s not what you expect. What do you expect, and what does actually happen?

Sorry, the details were in SO.

I am getting a response body of type gzip from the API. As resp.Body is a reader interface, I felt I should put that into the gunzip.NewReader to obtain a new decompressed json of type reader. My understanding was if I put this into the decoder with my struct, that would marshal the json into my structs. Unfortunately when I inspect the struct its empty. However, when I do a copy of the reader to stdio I can clearly see the data.

And you don’t get an error of any kind from the JSON unmarshal, and the data matches your expected format? What if you take that data from stdout, stick it in a bytes.Buffer and run the unmarshal on that?

There are no errors and when I step through with the debugger I dont see any errors. A person has offered some code on Stack Overflow and he said he tested it successfully, yet I still get the empty struct. I am running go 1.7 and im beginning to think this is a bug with the language.

The correct answer is available at Stack Overflow. This was a poor lack of understanding of Unmarshalling on my behalf. I’m not sure I fully understand, but I can at least work backwards now.

The solution can be seen here, thanks to Ezequiel Moreno.

play.golang.org/p/Vt07y_xgak

1 Like

:slight_smile:

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.