jsqr

package module
v0.0.0-...-00e7567 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2024 License: MIT Imports: 8 Imported by: 0

README

jsqr

Query or evaluate JSON values.

go get github.com/ofabricio/jsqr

Example

package main

import "github.com/ofabricio/jsqr"

func main() {

    j := `
        {
            "data": { "store": "Grocery" },
            "tags": [
                { "name": "Fruit", "items": [{ "name": "Apple" }] },
                { "name": "Snack", "items": [{ "name": "Chips" }] },
                { "name": "Drink", "items": [{ "name": "Water" }, { "name": "Wine" }] }
            ]
        }
    `

    a := jsqt.Get(j, `.data.store`)
    b := jsqt.Get(j, `.tags.[1].name == "Snack"`)
    c := jsqt.Get(j, `.tags.[ .name == "Drink" ].items.[0].name`)

    fmt.Println(a) // "Grocery"
    fmt.Println(b) // true
    fmt.Println(c) // "Water"
}

Documentation

Expression Description
. Returns the current context.
.a Returns a key value. For keys with characters other than [a-zA-Z0-9_] use ."a".
.a == 100 Boolean expression that returns either true or false.
.[0] Returns the array item at the index.
.[ .a > .b ] Returns the array item that matches the filter expression.
== != >= > <= < Comparison operators.
eq ne Case insensitive comparison operators.
& | Logical operators: .a == .b & .c == 100 | .d == true.
.(func) Calls a function. See functions below.

Functions

TODO

Documentation

Overview

Example (CompilerCompile)
package main

import (
	"bufio"
	"fmt"
	"strings"
)

func main() {

	s := `
		true
		false
		null
		123
		""

		.
		."a"
		.a
		."a"."b"
		.a.b
		.a0_B5_z99.世界.世.界
		.[0]

		.a == .b
		.a == .b & .c == .d
		.a == .b & .c == .d & .e == .f
		.a == .b | .c == .d
		.a == .b | .c == .d | .e == .f
		.a == .b & .c == .d | .e == .f
		.a == .b | .c == .d & .e == .f
		( .a == .b | .c == .d ) & .e == .f
		.a == .b | ( .c == .d & .e == .f )
		.a == .b | ( .c == .d & .e == .f )

		.a.[.a==.b].b.[.a==.b]
		.a.[ .a == .b ].b.[ .a == "b" ]

		.(upper)
		.(upper).(lower).[ .(upper) == "HI" ]
	`

	for _, line := range lines(s) {
		c := compiler{}
		v := c.Compile(line)
		fmt.Println(line)
		print(v.expr)
	}

}

func lines(v string) (lines []string) {
	for s := bufio.NewScanner(strings.NewReader(v)); s.Scan(); {
		if line := strings.TrimSpace(s.Text()); line != "" {
			lines = append(lines, line)
		}
	}
	return
}
Output:

true
[Bool] true
false
[Bool] false
null
[Null]
123
[Num] 123
""
[Str] ""
.
[Path]
    [This]
."a"
[Path]
    [Key]
        [Str] "a"
.a
[Path]
    [Idn] a
."a"."b"
[Path]
    [Key]
        [Str] "a"
    [Key]
        [Str] "b"
.a.b
[Path]
    [Idn] a
    [Idn] b
.a0_B5_z99.世界.世.界
[Path]
    [Idn] a0_B5_z99
    [Idn] 世界
    [Idn] 世
    [Idn] 界
.[0]
[Path]
    [Index] 0
.a == .b
[EQ]
    [Path]
        [Idn] a
    [Path]
        [Idn] b
.a == .b & .c == .d
[And]
    [EQ]
        [Path]
            [Idn] a
        [Path]
            [Idn] b
    [EQ]
        [Path]
            [Idn] c
        [Path]
            [Idn] d
.a == .b & .c == .d & .e == .f
[And]
    [EQ]
        [Path]
            [Idn] a
        [Path]
            [Idn] b
    [And]
        [EQ]
            [Path]
                [Idn] c
            [Path]
                [Idn] d
        [EQ]
            [Path]
                [Idn] e
            [Path]
                [Idn] f
.a == .b | .c == .d
[Or]
    [EQ]
        [Path]
            [Idn] a
        [Path]
            [Idn] b
    [EQ]
        [Path]
            [Idn] c
        [Path]
            [Idn] d
.a == .b | .c == .d | .e == .f
[Or]
    [EQ]
        [Path]
            [Idn] a
        [Path]
            [Idn] b
    [Or]
        [EQ]
            [Path]
                [Idn] c
            [Path]
                [Idn] d
        [EQ]
            [Path]
                [Idn] e
            [Path]
                [Idn] f
.a == .b & .c == .d | .e == .f
[Or]
    [And]
        [EQ]
            [Path]
                [Idn] a
            [Path]
                [Idn] b
        [EQ]
            [Path]
                [Idn] c
            [Path]
                [Idn] d
    [EQ]
        [Path]
            [Idn] e
        [Path]
            [Idn] f
.a == .b | .c == .d & .e == .f
[Or]
    [EQ]
        [Path]
            [Idn] a
        [Path]
            [Idn] b
    [And]
        [EQ]
            [Path]
                [Idn] c
            [Path]
                [Idn] d
        [EQ]
            [Path]
                [Idn] e
            [Path]
                [Idn] f
( .a == .b | .c == .d ) & .e == .f
[And]
    [Or]
        [EQ]
            [Path]
                [Idn] a
            [Path]
                [Idn] b
        [EQ]
            [Path]
                [Idn] c
            [Path]
                [Idn] d
    [EQ]
        [Path]
            [Idn] e
        [Path]
            [Idn] f
.a == .b | ( .c == .d & .e == .f )
[Or]
    [EQ]
        [Path]
            [Idn] a
        [Path]
            [Idn] b
    [And]
        [EQ]
            [Path]
                [Idn] c
            [Path]
                [Idn] d
        [EQ]
            [Path]
                [Idn] e
            [Path]
                [Idn] f
.a == .b | ( .c == .d & .e == .f )
[Or]
    [EQ]
        [Path]
            [Idn] a
        [Path]
            [Idn] b
    [And]
        [EQ]
            [Path]
                [Idn] c
            [Path]
                [Idn] d
        [EQ]
            [Path]
                [Idn] e
            [Path]
                [Idn] f
.a.[.a==.b].b.[.a==.b]
[Path]
    [Idn] a
    [Array]
        [EQ]
            [Path]
                [Idn] a
            [Path]
                [Idn] b
    [Idn] b
    [Array]
        [EQ]
            [Path]
                [Idn] a
            [Path]
                [Idn] b
.a.[ .a == .b ].b.[ .a == "b" ]
[Path]
    [Idn] a
    [Array]
        [EQ]
            [Path]
                [Idn] a
            [Path]
                [Idn] b
    [Idn] b
    [Array]
        [EQ]
            [Path]
                [Idn] a
            [Str] "b"
.(upper)
[Path]
    [Fun] upper
.(upper).(lower).[ .(upper) == "HI" ]
[Path]
    [Fun] upper
    [Fun] lower
    [Array]
        [EQ]
            [Path]
                [Fun] upper
            [Str] "HI"

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Expr

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

func Compile

func Compile(expr string) Expr

Compile parses an expression and returns, if successful, an Expr object that can be used to match against JSON.

type Json

type Json struct{ scan.Bytes }

func Get

func Get(jsn []byte, expr string) Json

Get applies expr to jsn and returns the resulting JSON. The JSON must be valid.

func (Json) Eq

func (a Json) Eq(b Json) bool

func (Json) Float64

func (a Json) Float64() float64

func (Json) GetIndex

func (j Json) GetIndex(idx int) Json

func (Json) GetKey

func (j Json) GetKey(key string) Json

func (Json) IsNumber

func (a Json) IsNumber() bool

func (*Json) IterArray

func (j *Json) IterArray() iter.Seq2[int, Json]

func (*Json) IterObject

func (j *Json) IterObject() iter.Seq2[string, Json]

Jump to

Keyboard shortcuts

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