• Stars
    star
    275
  • Rank 149,796 (Top 3 %)
  • Language
    Go
  • License
    Other
  • Created almost 9 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

JSON query expression library in Golang.

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

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

More Repositories

1

gojq

JSON query in Golang
Go
190
star
2

dns-zonefile

A DNS zone file generator and parser written in Javascript.
JavaScript
156
star
3

gosqljson

A Go library to work with SQL database
Go
63
star
4

filesync

Filesync is a utility written in Golang which helps you to keep the files on the client up to date with the files on the server.
Go
60
star
5

leanweb

Builds framework agnostic web components.
JavaScript
48
star
6

gosqlapi

Turns any SQL database into a RESTful API.
Go
45
star
7

ip6

IPv6 address helper utilities.
JavaScript
20
star
8

splitargs

Splits strings into tokens by given separator, treating quoted part as a single token.
JavaScript
17
star
9

gostrgen

Random string generator in Golang.
Go
16
star
10

gosqlcrud

A Go library to work with SQL database using standard `database/sql` api. It supports SQL to array/maps/structs, and CRUD operations on structs.
Go
12
star
11

nproxy

A nodejs proxy server.
JavaScript
10
star
12

parse5

Ported from inikulin/parse5 but for Deno.
JavaScript
9
star
13

FSK

This is an iOS based signal generator.
Objective-C
7
star
14

azui

A set of more desktop/mobile/touchscreen friendly web UI components.
JavaScript
7
star
15

gproxy

Go Proxy
Go
7
star
16

vpn-route

Sometime you just need it. F*** GFW.
6
star
17

curl-parser

curl command parser.
JavaScript
6
star
18

MyNotes

A collection of my frequently used scripts.
C++
4
star
19

mf_proxmox_kvm_scripts

Scripts for kvm templates for Modules Factory Proxmox WHMCS modules.
Shell
4
star
20

goweb

Multi domain/host web server written in Golang.
Go
4
star
21

gorest

A restful framework in Golang.
Go
3
star
22

yotta-node

Yotta DB is a local file based key value database, written in Node.js.
JavaScript
3
star
23

optional

If you think if err != nil is too much, this might be for you.
Go
3
star
24

metal_gpu

Working with Metal GPU in C++ and Swift
C++
2
star
25

gosplitargs

Splits strings into tokens by given separator except treating quoted part as a single token.
Go
2
star
26

url_shortener

Yet another url shortener.
JavaScript
2
star
27

mnist-view

View mnist images in command line, in Deno
TypeScript
2
star
28

images

CNN Proof of Concept.
JavaScript
2
star
29

wsl

Web SQL Lite (Golang)
Go
2
star
30

ip6.sh

ip6.sh website.
JavaScript
2
star
31

dnd

drag and drop
JavaScript
1
star
32

cpp_code

CPP code.
C++
1
star
33

leanweb-pub-sub-demo

This project demonstrates how Leanweb helps web components to talk to each other.
JavaScript
1
star
34

gr

A go restful service, extensible by interceptors.
Go
1
star
35

jwt

JWT encoder/decoder/verifier.
Go
1
star
36

sso-client

SSO client app powered by Leanweb.
JavaScript
1
star
37

exparser

An expression parser.
Go
1
star
38

go

Go game with reinforcement learning.
C++
1
star
39

server_monitor

Server monitor project based on Net Data.
Shell
1
star
40

jsstrgen

Random string generator in Javascript.
JavaScript
1
star
41

sso

SSO login app powered by Leanweb.
JavaScript
1
star
42

gorediscache

Go redis cache.
Go
1
star
43

ann

Artificial Neural Network Proof of Concept
TypeScript
1
star
44

websql

Run SQL over the web - Backend made easier.
Go
1
star
45

maze

An experimental project for reinforcement learning.
C++
1
star
46

dataslots

http/websocket data slots.
Go
1
star
47

secretrest

secretrest
C
1
star
48

ui

A css only ui library.
JavaScript
1
star
49

nn

World simplest neural network.
C++
1
star
50

website-seed

Web site seed.
CSS
1
star
51

netdata

Backend of netdata.io.
Go
1
star
52

sgf2go-cpp

SGF Go game parser and generator.
C++
1
star
53

gammu-gateway

A gammu SMS service gateway.
Go
1
star
54

large-comments

A VS Code plugin that generate large comments.
TypeScript
1
star
55

leanweb.app

leanweb.app website source code.
JavaScript
1
star
56

wsld

wsl daemon
Go
1
star
57

archlinux_scripts

Arch Linux Scripts
Shell
1
star
58

netdata2

An SQL backend for the web.
Go
1
star
59

signal

A command and broadcast based multi channel message hub.
Go
1
star
60

sgf2go

SGF Go game parser and generator.
JavaScript
1
star
61

gorest2

This is to continue gorest with incompatible changes.
Go
1
star
62

byoc

Bring your own cloud.
Go
1
star
63

ppw

Pixel Perfect Works.
JavaScript
1
star