jsonql

package module
v0.0.0-...-11504e6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 21, 2026 License: ISC Imports: 9 Imported by: 7 modules, 10 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

jsonql

JSON query expression library in Golang.

This library enables query against JSON. Currently supported operators are: (precedences from low to high)

||
&&
= != > < >= <= ~= !~= is isnot contains
+ -
* / %
^
( )

The following are the operator mappings to SQL:

  • AND to &&
  • OR to ||
  • RLIKE to ~=
  • NOT RLIKE to !~=

Install

go get -u github.com/elgs/jsonql

Example

package main

import (
	"fmt"

	"github.com/elgs/jsonql"
)

var jsonString = `
[
  {
    "name": "elgs",
    "gender": "m",
    "age": 35,
    "skills": [
      "Golang",
      "Java",
      "C"
    ]
  },
  {
    "name": "enny",
    "gender": "f",
    "age": 36,
    "hobby": null,
    "skills": [
      "IC",
      "Electric design",
      "Verification"
    ]
  },
  {
    "name": "sam",
    "gender": "m",
    "age": 1,
    "hobby": "dancing",
    "skills": [
      "Eating",
      "Sleeping",
      "Crawling"
    ]
  }
]
`

func main() {
	parser, err := jsonql.NewStringQuery(jsonString)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(parser.Query("name='elgs'"))
	//[map[skills:[Golang Java C] name:elgs gender:m age:35]] <nil>

	fmt.Println(parser.Query("name='elgs' && gender='f'"))
	//[] <nil>

	fmt.Println(parser.Query("age<10 || (name='enny' && gender='f')"))
	// [map[hobby:<nil> skills:[IC Electric design Verification] name:enny gender:f age:36] map[name:sam gender:m age:1 hobby:dancing skills:[Eating Sleeping Crawling]]] <nil>

	fmt.Println(parser.Query("age<10"))
	// [map[gender:m age:1 hobby:dancing skills:[Eating Sleeping Crawling] name:sam]] <nil>

	fmt.Println(parser.Query("1=0"))
	//[] <nil>

	fmt.Println(parser.Query("age=(2*3)^2"))
	//[map[skills:[IC Electric design Verification] name:enny gender:f age:36 hobby:<nil>]] <nil>

	fmt.Println(parser.Query("name ~= 'e.*'"))
	// [map[name:elgs gender:m age:35 skills:[Golang Java C]] map[hobby:<nil> skills:[IC Electric design Verification] name:enny gender:f age:36]] <nil>

	fmt.Println(parser.Query("name='el'+'gs'"))
	fmt.Println(parser.Query("age=30+5.0"))
	fmt.Println(parser.Query("age=40.0-5"))
	fmt.Println(parser.Query("age=70-5*7"))
	fmt.Println(parser.Query("age=70.0/2.0"))
	fmt.Println(parser.Query("age=71%36"))
	// [map[name:elgs gender:m age:35 skills:[Golang Java C]]] <nil>

	fmt.Println(parser.Query("hobby is defined"))
	// [map[name:enny gender:f age:36 hobby:<nil> skills:[IC Electric design Verification]] map[name:sam gender:m age:1 hobby:dancing skills:[Eating Sleeping Crawling]]] <nil>

	fmt.Println(parser.Query("hobby isnot defined"))
	// [map[name:sam gender:m age:1 skills:[Eating Sleeping Crawling]]] <nil>

	fmt.Println(parser.Query("hobby is null"))
	// [map[hobby:<nil> skills:[IC Electric design Verification] name:enny gender:f age:36]] <nil>

	fmt.Println(parser.Query("hobby isnot null"))
	// [map[name:sam gender:m age:1 hobby:dancing skills:[Eating Sleeping Crawling]]] <nil>

	fmt.Println(parser.Query("skills contains 'Eating'"))
	// [map[age:1 gender:m hobby:dancing name:sam skills:[Eating Sleeping Crawling]]] <nil>
}

Query Expressions

For details of query expressions, please read: https://github.com/elgs/gojq

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CompareSlices

func CompareSlices(a, b []string) bool

CompareSlices - compares to slices, return false if they are not equal, true if they are.

func ReverseString

func ReverseString(input string) string

ReverseString - reverses a string.

Types

type Element

type Element struct {
	// contains filtered or unexported fields
}

Element - an item in the stack

type JSONQL

type JSONQL struct {
	Data interface{}
}

JSONQL - JSON Query Lang struct encapsulating the JSON data.

func NewQuery

func NewQuery(jsonObject interface{}) *JSONQL

NewQuery - creates a new &JSONQL from an array of interface{} or a map of [string]interface{}

func NewStringQuery

func NewStringQuery(jsonString string) (*JSONQL, error)

NewStringQuery - creates a new &JSONQL from raw JSON string

func (*JSONQL) Query

func (thisJSONQL *JSONQL) Query(where string) (interface{}, error)

Query - queries against the JSON using the conditions specified in the where stirng.

type Lifo

type Lifo struct {
	// contains filtered or unexported fields
}

Lifo - last in first out stack

func (*Lifo) Len

func (s *Lifo) Len() int

Len - gets the length of the stack.

func (*Lifo) Peep

func (s *Lifo) Peep() (value interface{})

Peep - gets the last value in the stack without popping it out.

func (*Lifo) Pop

func (s *Lifo) Pop() (value interface{})

Pop - pops the last value out of the stack.

func (*Lifo) Print

func (s *Lifo) Print()

Print - shows what's in the stack.

func (*Lifo) Push

func (s *Lifo) Push(value interface{})

Push - pushes the value into the stack.

type Operator

type Operator struct {
	Precedence int
	Eval       func(symbolTable interface{}, left string, right string) (string, error)
}

Operator - encapsulates the Precedence and behavior logic of the operators.

type Parser

type Parser struct {
	Operators   map[string]*Operator
	SymbolTable interface{}
	// contains filtered or unexported fields
}

Parser - the main struct that contains operators, symbol table.

func (*Parser) Calculate

func (thisParser *Parser) Calculate(expression string) (string, error)

Calculate - gets the final result of the expression

func (*Parser) Evaluate

func (thisParser *Parser) Evaluate(ts *Lifo, postfix bool) (string, error)

Evaluate - evaluates the token stack until only one (as the final result) is left.

func (*Parser) Init

func (thisParser *Parser) Init()

Init - init inspects the Operators and learns how long the longest operator string is

func (*Parser) ParseRPN

func (thisParser *Parser) ParseRPN(tokens []string) (output *Lifo, err error)

ParseRPN - parses the RPN tokens

func (*Parser) Tokenize

func (thisParser *Parser) Tokenize(exp string) (tokens []string)

Tokenize - splits the expression into tokens.

type Stack

type Stack interface {
	Len() int
	Push(value interface{})
	Pop() (value interface{})
	Peep() (value interface{})
	Print()
}

Stack - a set of functions of the Stack

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL