This tutorial will follow my progress working on a library that would handle working with dynamic JSON data. Go is a great language for decoding JSON when the data layout is fixed, but since Go is a static language, dealing with JSON that is dynamic can be less intuitive than when working with dynamic languages like PHP, Ruby, or Python. This tutorial assumes that you have some working knowledge of Go. If you have no prior knowledge of Go, go here and here.
<rant>
I think Go is lacking in regards to the amount of intermediate and advanced examples that show how Go works solving actual problems instead of a "10000 foot view"of Go. I have great respect for the Go team and in particular, I think Andrew Gerrand is doing a great job of representing Go accross the globe, but if I see one more video of Andrew demoing chat roulette, I will commit Seppuku.
</rant>
Taking a whack at it
This is the sample JSON data from the Github API I am working with. And here is a sample JSON record.
Looking at the events data, I found that the payload object is different depending on the "type" of event. Immediately, I wanted to go back to my PHP roots and use $event = json_decode($source) and this problem would be solved, but the following example uses the most basic Go code.
The problem with this is you can only access top level data items. For instance, I can access the payload by using m["type"] but trying to get m["payload"]["description"] will not compile because an interface does not have any indexes. So what can we do to clean this up? I created a couple of structs and some basic functions to help me decode the JSON. I wanted a JSONCollection struct which contained the entire document and JSONObject which would be the struct used to read the results.
This still has the same limitation as before. I can use "Get" to find a top level attribute, but If I want a nested attribute, I have to type cast the interface to a map again and then grab the description out of that map. I need to come up with a better solution. The goal is to have syntax like the following.
This will give us a cleaner syntax to get at nested variables. So let's fix the code to allow this.
This gives a start to our library. Now we can retrieve strings of data (and nested data) with a minimal amount of Go code. Here is a an example as to how we would use this library.
Current Limitations
The library uses only strings, so if you have an integer or float, those values will have trouble converting to strings (I will fix this in a later blog post). You will have trouble traversing nested slices as that this will cause an error. Currently the example is read-only, as that was all that was initially needed.
Coming Up on Personal Ramblings
I want to make this a feature complete library, so here is a list of things that I want to add to this library.
- Get JSON Object by Position
- Get JSON Objects by using limit/offset
- Filter JSON Results
- Handling different variable types
- Creating Tests
- Creating a better Iterator
- Using Godoc
- Using GoFmt
ReplyDeleteWhen I initially commented, I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get several emails with the same comment. Is there any way you can remove people from that service? Thanks.
Amazon Web Services Training in OMR , Chennai | Best AWS Training in OMR,Chennai
Amazon Web Services Training in Tambaram, Chennai|Best AWS Training in Tambaram, Chennai
Read all the information that i've given in above article. It'll give u the whole idea about it.
ReplyDeleteangularjs Training in bangalore
angularjs Training in btm
angularjs Training in electronic-city
angularjs Training in online
angularjs Training in marathahalli
Read all the information that i've given in above article. It'll give u the whole idea about it.
ReplyDeleteangularjs Training in bangalore
angularjs Training in btm
angularjs Training in electronic-city
angularjs Training in online
angularjs Training in marathahalli
Excellant post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
ReplyDeleteonline Python certification course | python training in OMR | Python training course in Chennai
It is better to engaged ourselves in activities we like. I liked the post. Thanks for sharing.
ReplyDeleteJava training in Chennai | Java training in Bangalore
Java online training | Java training in Pune
This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me..
ReplyDeletebest rpa training in chennai | rpa online training |
rpa training in chennai |
rpa training in bangalore
rpa training in pune
rpa training in marathahalli
rpa training in btm
Keep writing wonderful blogs like this, I would like to read more blogs.
ReplyDeleteSelenium Training in Chennai
Best Selenium Training Institute in Chennai
ios developer training in chennai
.Net coaching centre in chennai
French Classes in Chennai
Big Data Training in Chennai
Best JAVA Training in Chennai
PHP Institutes in Chennai
PHP Training Center in Chennai
Awesome..You have clearly explained …Its very useful for me to know about new things..Keep on blogging..
ReplyDeleteDevops Training in Chennai | Devops Training Institute in Chennai
This is very good content you share on this blog. it's very informative and provide me future related information.
ReplyDeletedevops online training
aws online training
data science with python online training
data science online training
rpa online training
ReplyDeleteThis is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.I like it and help me to article very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.
DedicatedHosting4u.com
I like the helpful info you provide in your articles. I’ll bookmark your weblog and check again here regularly. I am quite sure I will learn much new stuff right here! Good luck for the next!
ReplyDeleteDot Net Training in Chennai | Dot Net Training in anna nagar | Dot Net Training in omr | Dot Net Training in porur | Dot Net Training in tambaram | Dot Net Training in velachery