Query JSON data using Golang (gojsonq)

Saddam H
2 min readMay 29, 2018

gojsonq (A simple Go package to Query over JSON Data)

Most often developer needs to consume JSON data from other service and query over them. Querying JSON data is little time-consuming. For the last few days, I was working on a package (gojsonq) for Golang to query JSON data easily. The idea and inspiration comes from PHP-JSONQ by Nahid Bin Azhar.

Let's take a sample JSON data to start with:

sample-data.json

Now we are ready to query the data, let's see some examples

Example 1

Query: select * from vendor.items where price > 1200 or id null

Using gojsonq we can do the query like:

example-1.go

Example 2

Query: select name, price from vendor.items where price > 1200 or id null

Using gojsonq we can do the query like:

example-2.go

Example 3

Query: select sum(price) from vendor.items where price > 1200 or id null

Using gojsonq we can do the query like:

example-3.go

Note you can use other aggregation function like Min/Max/Avg/Count`

Example 4

Query: select price from vendor.items where price > 1200

Using gojsonq we can do the query like:

example-4.go

Note: You’ll get a plain array of price

Example 5

Query: select * from vendor.items order by price

Using gojsonq we can do the query like:

example-5.go

Example 6

Query: select description from .

Using gojsonq we can do the query like:

example-6.go

Example 7

Let's find a deeply nested property and handle error properly:

example-7.go full example

There are some other useful methods available in the package. The package is still under heavy development! If you have any suggestion or bug report don’t forget to open an issue

If you are not using Go, you can also use the same implementation of the package using PHP, Javascript, Python and Kotlin. Links are listed below:

If you like the idea don’t forget to give a STAR :)

Github link for gojsonq

Thank you very much for reading the article

--

--

Saddam H

Software Engineer, Pathao Inc | Open Source Enthusiast | Love to write elegant code | Gopher by passion | https://thedevsaddam.github.io