I am trying to decode byte array but am unable to unmarshal data into struct. I get an error when trying to access index 1 of array. Can anyone see the error?
Checking a return value of error should be a quite well known thing and is a good habbit, while panicing on it is not but good enough for such a small piece of code. To be honest, I took this for granted, and wanted to start with only mentioning that without even trying your code.
The other technique used, was just inspired by google, entering “json validator” into the search box and took the first one. There was not much more into this…
At the end, if you check error returns and know how to use google, most things are easily solvable. What makes debugging hard, is to ignore half of the available data.
I never used a JSON validator before, so thank you. I will take your advise on starting with return value of error as the start of debugging process. Interestingly enough, the REST API I am consuming data from gave the JSON snippet in their documentation with those errors. However, when I tried to unmarshal the data straight from a request from their API, it worked.
If you’re handing some data in some format, it’s best to make sure the data is valid by using external validators. For example, you could’ve piped that JSON through python -mjson.tool to see if it outputs the same JSON back. If it does, then proceed with the debugging of your code!
In your application you can use https://github.com/clbanning/checkjson to validate input. Or you can use the ResolveJSONError function to just get better error info after calling json.Unmarshal.