Tukki used struggle!

June 28, 2018, 5:30 a.m.

I just always seem to be struggling when it comes to making a backend with anything else than Node. In the last post I said that it would be easy to do the other services with Go since I already made one and they're basically the same. Well they are not and I was wrong.

In theory they are very much the same. Just some CRUD functionality around different structs. So why is it so hard? That's because of my good old friend JSON. I wrote already about the difficulties handling JSON with Java and now I am struggling with same kind of problems wtith Go and JSON.

The problems arose when I tried to receive a deeply nested JSON and save that to DB. What is even more irritating is that I do not need to map it to anything but just save it to database. I do map outermost JSON to a struct but the nested part doesn't need to be mapped to anything.

{ "property 1": "key1", "proeprty2": "key2", "jsonArray1": [ { "object1": "key1", "jsonArray2": [ { "object2": "key2" } ]  }]

The JSON in question looks a bit like that but with more properties and more stuff inside the arrays.

The first approach I tried is that I made a struct where I had one property with type of json.RawMessage with the hopes that it would work since it is just saved to database and nothing else. Obviously that didn't work since I am writing this post. PostgreSQL just gave me error about a malformed array and I couldn't get around it. After that I have tried having structs for those JSON arrays and after that I tried nested structs. I can't remember the errors I had (they're in my error log though!) but I just couldn't get it to work which is really frustrating. On the surface it looks like a rather simple problem to solve but yet I just don't seem to be able to crack it. At this point I would just really love to get my services up and running and start working on the frontend with React.