A Go library that converts JSON Logic expressions into SQL. This library provides a clean, type-safe API for transforming JSON Logic rules into SQL WHERE clauses or standalone conditions, with support for multiple SQL dialects.
- Complete JSON Logic Support: Implements all core JSON Logic operators
- SQL Dialect Support: Target BigQuery, Spanner, PostgreSQL, DuckDB, or ClickHouse
- Custom Operators: Extensible registry pattern for custom SQL functions
- Schema Validation: Optional field schema for strict column validation
- Structured Errors: Error codes and JSONPath locations for debugging
- Library & CLI: Both programmatic API and interactive REPL
go get github.com/h22rana/jsonlogic2sql@latestpackage main
import (
"fmt"
"github.com/h22rana/jsonlogic2sql"
)
func main() {
sql, err := jsonlogic2sql.Transpile(
jsonlogic2sql.DialectBigQuery,
`{">": [{"var": "amount"}, 1000]}`,
)
if err != nil {
panic(err)
}
fmt.Println(sql) // Output: WHERE amount > 1000
}| Category | Operators |
|---|---|
| Data Access | var, missing, missing_some |
| Comparison | ==, ===, !=, !==, >, >=, <, <= |
| Logical | and, or, !, !!, if |
| Numeric | +, -, *, /, %, max, min |
| Array | in, map, filter, reduce, all, some, none, merge |
| String | in, cat, substr |
| Dialect | Constant |
|---|---|
| Google BigQuery | DialectBigQuery |
| Google Cloud Spanner | DialectSpanner |
| PostgreSQL | DialectPostgreSQL |
| DuckDB | DialectDuckDB |
| ClickHouse | DialectClickHouse |
- Getting Started - Installation and basic usage
- Operators - All supported operators with examples
- SQL Dialects - Dialect-specific SQL generation
- Custom Operators - Extend with your own operators
- Schema Validation - Field validation and type checking
- Examples - Comprehensive examples
- API Reference - Full API documentation
- Error Handling - Error codes and programmatic handling
- Development - Contributing and development guide
- REPL - Interactive testing tool
Semantic Correctness Assumption: This library assumes that the input JSONLogic is semantically correct. The transpiler generates SQL that directly corresponds to the JSONLogic structure without validating the logical correctness of the expressions.
SQL Injection: This library does NOT handle SQL injection prevention. The caller is responsible for validating input and using parameterized queries where appropriate.
make run[BigQuery] jsonlogic> {">": [{"var": "amount"}, 1000]}
SQL: WHERE amount > 1000
[BigQuery] jsonlogic> :dialect
Select dialect: PostgreSQL
[PostgreSQL] jsonlogic> {"merge": [{"var": "a"}, {"var": "b"}]}
SQL: WHERE (a || b)
make test # Run all tests (3,000+ test cases)
make build # Build REPL binary
make lint # Run linter
make run # Run REPLThis project is licensed under the MIT License - see the LICENSE file for details.