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:
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 2
Query: select name, price from vendor.items where price > 1200 or id null
Using gojsonq we can do the query like:
Example 3
Query: select sum(price) from vendor.items where price > 1200 or id null
Using gojsonq we can do the query like:
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:
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 6
Query: select description from .
Using gojsonq we can do the query like:
Example 7
Let's find a deeply nested property and handle error properly:
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