Top Related Projects
Get JSON values quickly - JSON parser for Go
One of the fastest alternative JSON parser for Go that does not require schema
Fast JSON parser and validator for Go. No custom structs, no code generation, no reflection
Fast JSON serializer for golang.
For parsing, creating and editing unknown or dynamic JSON in Go
Quick Overview
GOJSONQ is a simple Go package that allows querying JSON data using a fluent interface. It provides a convenient way to extract, manipulate, and transform JSON data without writing complex parsing logic. The library supports various operations like filtering, sorting, and aggregating JSON data.
Pros
- Easy to use with a chainable, fluent API
- Supports complex queries and nested JSON structures
- Lightweight with no external dependencies
- Provides caching mechanism for improved performance
Cons
- Limited to JSON data format only
- May not be as performant as more specialized JSON parsing libraries for large datasets
- Documentation could be more comprehensive with additional examples
Code Examples
- Basic query to filter and select data:
import "github.com/thedevsaddam/gojsonq/v2"
json := `{"users": [{"id": 1, "name": "John"}, {"id": 2, "name": "Jane"}]}`
result := gojsonq.New().FromString(json).
From("users").
Where("id", ">", 1).
Select("name").
Get()
// Result: [{"name": "Jane"}]
- Aggregation example:
json := `{"products": [{"price": 100}, {"price": 200}, {"price": 300}]}`
result := gojsonq.New().FromString(json).
From("products").
Sum("price")
// Result: 600
- Nested query with multiple conditions:
json := `{"users": [{"name": "John", "age": 30, "skills": ["go", "python"]},
{"name": "Jane", "age": 25, "skills": ["java", "ruby"]}]}`
result := gojsonq.New().FromString(json).
From("users").
Where("age", ">", 20).
Where("skills", "contains", "go").
Get()
// Result: [{"name": "John", "age": 30, "skills": ["go", "python"]}]
Getting Started
To use GOJSONQ in your Go project, follow these steps:
-
Install the package:
go get github.com/thedevsaddam/gojsonq/v2
-
Import the package in your Go file:
import "github.com/thedevsaddam/gojsonq/v2"
-
Create a new instance and start querying:
jq := gojsonq.New().FromString(jsonString) result := jq.From("users").Where("age", ">", 18).Get()
Competitor Comparisons
Get JSON values quickly - JSON parser for Go
Pros of gjson
- Faster performance, especially for large JSON files
- Supports a wider range of JSON operations, including JSON Lines and JSON Streaming
- More lightweight and focused on JSON parsing and querying
Cons of gjson
- Less flexible query syntax compared to gojsonq
- Lacks advanced features like aggregation and sorting
- No built-in support for writing or modifying JSON data
Code Comparison
gojsonq example:
result := gojsonq.New().File("data.json").
From("users").
Where("age", ">", 30).
Select("name", "email").
Get()
gjson example:
data, _ := ioutil.ReadFile("data.json")
result := gjson.Get(string(data), "users.#(age>30)#.name|email")
Both libraries offer efficient ways to query JSON data, but gojsonq provides a more SQL-like syntax, while gjson uses a compact path syntax. gjson is generally faster and more lightweight, making it ideal for simple queries and large datasets. gojsonq offers more advanced features and a more intuitive query structure for complex operations.
One of the fastest alternative JSON parser for Go that does not require schema
Pros of jsonparser
- Faster performance, especially for large JSON files
- Lower memory usage due to not parsing the entire JSON structure
- Supports streaming JSON parsing for handling large datasets
Cons of jsonparser
- Less intuitive API, requiring more manual work for complex queries
- Limited functionality compared to gojsonq's rich query features
- No built-in support for aggregations or advanced filtering
Code Comparison
gojsonq example:
result := gojsonq.New().File("data.json").
From("users").
Where("age", ">", 30).
Select("name", "email").
Get()
jsonparser example:
var users []User
jsonparser.ArrayEach(data, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
age, _ := jsonparser.GetInt(value, "age")
if age > 30 {
name, _ := jsonparser.GetString(value, "name")
email, _ := jsonparser.GetString(value, "email")
users = append(users, User{Name: name, Email: email})
}
}, "users")
The code comparison demonstrates that gojsonq provides a more declarative and concise approach to querying JSON data, while jsonparser requires more manual implementation but offers finer control over parsing and memory usage. gojsonq is better suited for simple to moderately complex queries, while jsonparser excels in performance-critical scenarios or when dealing with very large JSON files.
Fast JSON parser and validator for Go. No custom structs, no code generation, no reflection
Pros of fastjson
- Significantly faster JSON parsing and serialization
- Lower memory usage and fewer allocations
- Supports streaming JSON parsing for large files
Cons of fastjson
- Less feature-rich query capabilities compared to gojsonq
- Steeper learning curve due to low-level API
- Limited built-in data manipulation functions
Code Comparison
gojsonq example:
result := gojsonq.New().File("data.json").From("users").Where("age", ">", 30).Get()
fastjson example:
var p fastjson.Parser
v, err := p.Parse(jsonData)
users := v.GetArray("users")
for _, user := range users {
if age := user.GetInt("age"); age > 30 {
// Process user
}
}
Summary
fastjson excels in performance and efficiency, making it ideal for high-throughput applications or working with large JSON datasets. It offers a low-level API that provides fine-grained control over parsing and manipulation.
gojsonq, on the other hand, provides a more user-friendly interface with powerful query capabilities, making it easier to extract and manipulate JSON data. It's better suited for applications where ease of use and expressive queries are prioritized over raw performance.
Choose fastjson for performance-critical applications or when working with large JSON streams. Opt for gojsonq when you need a simple, expressive way to query and manipulate JSON data in your Go applications.
Fast JSON serializer for golang.
Pros of easyjson
- Focuses on high-performance JSON encoding/decoding
- Generates code for faster serialization/deserialization
- Supports custom MarshalJSON/UnmarshalJSON methods
Cons of easyjson
- Limited to JSON processing, lacks query capabilities
- Requires code generation step, increasing build complexity
- Less flexible for dynamic JSON manipulation
Code Comparison
easyjson:
//easyjson:json
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
}
gojsonq:
result := gojsonq.New().File("data.json").
From("users").
Where("age", ">", 18).
Select("name", "age").
Get()
Key Differences
- Purpose: easyjson focuses on fast JSON processing, while gojsonq provides JSON querying capabilities
- Performance: easyjson offers better performance for JSON encoding/decoding
- Flexibility: gojsonq allows for more dynamic JSON manipulation and querying
- Usage: easyjson requires code generation, gojsonq works with runtime queries
- Scope: easyjson is specific to JSON, gojsonq supports both JSON and YAML
Both libraries serve different purposes within the Go ecosystem. easyjson is ideal for applications requiring high-performance JSON processing, while gojsonq is better suited for projects needing flexible JSON querying and manipulation.
For parsing, creating and editing unknown or dynamic JSON in Go
Pros of gabs
- More flexible and dynamic JSON manipulation, allowing for creation and modification of complex structures
- Better performance for large JSON documents due to its underlying data structure
- Supports both encoding and decoding of JSON data
Cons of gabs
- Less intuitive query syntax compared to gojsonq's fluent interface
- Lacks some advanced querying features like conditional filtering and aggregation
- May require more manual handling for certain operations
Code Comparison
gabs:
json, _ := gabs.ParseJSON([]byte(`{"foo":{"bar":"baz"}}`))
value, _ := json.Path("foo.bar").Data().(string)
json.Set(42, "foo", "number")
gojsonq:
value := gojsonq.New().FromString(`{"foo":{"bar":"baz"}}`).
Find("foo.bar").(string)
result := gojsonq.New().FromString(`{"foo":{"bar":"baz"}}`).
From("foo").Where("bar", "=", "baz").Get()
Both libraries offer JSON parsing and querying capabilities, but gabs focuses more on dynamic manipulation, while gojsonq provides a more intuitive querying interface. gabs is generally better for complex JSON modifications, while gojsonq excels in data extraction and filtering operations.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
A simple Go package to Query over JSON Data. It provides simple, elegant and fast ODM like API to access, query JSON document
Installation
Install the package using
$ go get github.com/thedevsaddam/gojsonq/v2
Usage
To use the package import it in your *.go
code
import "github.com/thedevsaddam/gojsonq/v2"
Let's see a quick example:
package main
import gojsonq "github.com/thedevsaddam/gojsonq/v2"
func main() {
const json = `{"name":{"first":"Tom","last":"Hanks"},"age":61}`
name := gojsonq.New().FromString(json).Find("name.first")
println(name.(string)) // Tom
}
Another example:
package main
import (
"fmt"
gojsonq "github.com/thedevsaddam/gojsonq/v2"
)
func main() {
const json = `{"city":"dhaka","type":"weekly","temperatures":[30,39.9,35.4,33.5,31.6,33.2,30.7]}`
avg := gojsonq.New().FromString(json).From("temperatures").Avg()
fmt.Printf("Average temperature: %.2f", avg) // 33.471428571428575
}
You can query your document using the various query methods such as Find, First, Nth, Pluck, Where, OrWhere, WhereIn, WhereStartsWith, WhereEndsWith, WhereContains, Sort, GroupBy, SortBy and so on. Also you can aggregate data after query using Avg, Count, Max, Min, Sum etc.
Find more query API in Wiki page
Bugs and Issues
If you encounter any bugs or issues, feel free to open an issue at github.
Also, you can shoot me an email to mailto:thedevsaddam@gmail.com for hugs or bugs.
Credit
Special thanks to Nahid Bin Azhar for the inspiration and guidance for the package. Thanks to Ahmed Shamim Hasan Shaon for his support from the very beginning.
Contributors
Contribution
If you are interested to make the package better please send pull requests or create an issue so that others can fix. Read the contribution guide here
License
The gojsonq is an open-source software licensed under the MIT License.
Top Related Projects
Get JSON values quickly - JSON parser for Go
One of the fastest alternative JSON parser for Go that does not require schema
Fast JSON parser and validator for Go. No custom structs, no code generation, no reflection
Fast JSON serializer for golang.
For parsing, creating and editing unknown or dynamic JSON in Go
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot