Developers often need to consume JSON data from other services and query over it. However, querying JSON data can be time-consuming. Over the past few days, I’ve been working on a Golang package to make querying JSON data easier. The idea and inspiration for this package come from PHP-JSONQ by Nahid Bin Azhar. Let’s start by looking at a sample JSON dataset:
Now that we have the JSON data, let’s dive into some examples of how to query it using the package.
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:
The package also includes several other useful methods. Please note that the package is still under active development! If you have any suggestions or encounter any bugs, feel free to open an issue.
If you like the concept, don’t forget to give it a STAR!
Thank you very much for reading the article!