Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/elgs/jsonql
JSON query expression library in Golang.
https://github.com/elgs/jsonql
Last synced: 3 months ago
JSON representation
JSON query expression library in Golang.
- Host: GitHub
- URL: https://github.com/elgs/jsonql
- Owner: elgs
- License: other
- Created: 2015-12-29T11:24:04.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2020-11-20T03:19:00.000Z (about 4 years ago)
- Last Synced: 2024-07-31T20:52:46.990Z (6 months ago)
- Language: Go
- Homepage:
- Size: 42 KB
- Stars: 274
- Watchers: 12
- Forks: 40
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - jsonql - JSON query expression library in Golang. (Query Language / HTTP Clients)
- zero-alloc-awesome-go - jsonql - JSON query expression library in Golang. (Query Language / HTTP Clients)
- awesome-go - jsonql - JSON query expression library in Golang. - ★ 177 (Query Language)
- awesome-go-extra - jsonql - 12-29T11:24:04Z|2020-11-20T03:19:00Z| (Query Language / HTTP Clients)
- awesome-go-zh - jsonql
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 mapings to SQL:
* `AND` to `&&`
* `OR` to `||`
* `RLIKE` to `~=`
* `NOT RLIKE` to `!~=`## Install
`go get -u github.com/elgs/jsonql`## Example
```go
package mainimport (
"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]]fmt.Println(parser.Query("name='elgs' && gender='f'"))
//[]fmt.Println(parser.Query("age<10 || (name='enny' && gender='f')"))
// [map[hobby: skills:[IC Electric design Verification] name:enny gender:f age:36] map[name:sam gender:m age:1 hobby:dancing skills:[Eating Sleeping Crawling]]]fmt.Println(parser.Query("age<10"))
// [map[gender:m age:1 hobby:dancing skills:[Eating Sleeping Crawling] name:sam]]fmt.Println(parser.Query("1=0"))
//[]fmt.Println(parser.Query("age=(2*3)^2"))
//[map[skills:[IC Electric design Verification] name:enny gender:f age:36 hobby:]]fmt.Println(parser.Query("name ~= 'e.*'"))
// [map[name:elgs gender:m age:35 skills:[Golang Java C]] map[hobby: skills:[IC Electric design Verification] name:enny gender:f age:36]]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]]]fmt.Println(parser.Query("hobby is defined"))
// [map[name:enny gender:f age:36 hobby: skills:[IC Electric design Verification]] map[name:sam gender:m age:1 hobby:dancing skills:[Eating Sleeping Crawling]]]fmt.Println(parser.Query("hobby isnot defined"))
// [map[name:sam gender:m age:1 skills:[Eating Sleeping Crawling]]]fmt.Println(parser.Query("hobby is null"))
// [map[hobby: skills:[IC Electric design Verification] name:enny gender:f age:36]]fmt.Println(parser.Query("hobby isnot null"))
// [map[name:sam gender:m age:1 hobby:dancing skills:[Eating Sleeping Crawling]]]fmt.Println(parser.Query("skills contains 'Eating'"))
// [map[age:1 gender:m hobby:dancing name:sam skills:[Eating Sleeping Crawling]]]
}
```## Query Expressions
For details of query expressions, please read: https://github.com/elgs/gojq