gojq

package module
v0.6.1-0...-374bbaf Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2019 License: MIT Imports: 18 Imported by: 0

README

gojq CI Status

Pure Go implementation of jq.

Usage

 $ echo '{"foo": 128}' | gojq '.foo'
128
 $ echo '{"a": {"b": 42}}' | gojq '.a.b'
42
 $ echo '{"id": "sample", "10": {"b": 42}}' | gojq '{(.id): .["10"].b}'
{
  "sample": 42
}
 $ echo '[{"id":1},{"id":2},{"id":3}]' | gojq '.[] | .id'
1
2
3
 $ echo '{"a":1,"b":2}' | gojq '.a += 1 | .b *= 2'
{
  "a": 2,
  "b": 4
}
 $ echo '{"a":1} [2] 3' | gojq '. as {$a} ?// [$a] ?// $a | $a'
1
2
3
 $ echo '{"foo": 4722366482869645213696}' | gojq .foo
4722366482869645213696  # keeps the precision of number while jq does not
 $ gojq -n 'def fact($n): if $n < 1 then 1 else $n * fact($n - 1) end; fact(50)'
30414093201713378043612608166064768844377641568960512000000000000 # arbitrary-precision integer calculation

Nice error messages.

 $ echo '[1,2,3]' | gojq  '.foo & .bar'
gojq: invalid query: .foo & .bar
    .foo & .bar
         ^  unexpected token "&"
 $ echo '{"foo": { bar: [] } }' | gojq '.'
gojq: invalid json: <stdin>
    {"foo": { bar: [] } }
              ^  invalid character 'b' looking for beginning of object key string

Installation

Homebrew
brew install itchyny/tap/gojq
Build from source
go get -u github.com/itchyny/gojq/cmd/gojq

Difference to jq

  • gojq is purely implemented with Go language and is completely portable. jq depends on the C standard library so the availability of math functions depends on the library. jq also depends on the regular expression library and it makes build scripts complex.
  • gojq implements nice error messages for invalid query and JSON input. The error message of jq is sometimes difficult to tell where to fix the query.
  • gojq does not keep the order of object keys. I understand this might cause problems for some scripts but basically we should not rely on the order of object keys. I would implement when ordered map is implemented in the standard library of Go but I'm less motivated.
  • gojq supports arbitrary-precision integer calculation while jq does not. This is important to keeping the precision of numeric IDs or nanosecond values. You can use gojq to solve some mathematical problems which require big integers.

Bug Tracker

Report bug at Issues・itchyny/gojq - GitHub.

Author

itchyny (https://github.com/itchyny)

License

This software is released under the MIT License, see LICENSE.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Alt

type Alt struct {
	Left  *Expr      `@@`
	Right []AltRight `@@*`
}

Alt ...

func (*Alt) String

func (e *Alt) String() string

type AltRight

type AltRight struct {
	Op    Operator `@"//"`
	Right *Expr    `@@`
}

AltRight ...

func (AltRight) String

func (e AltRight) String() string

type AndExpr

type AndExpr struct {
	Left  *Compare       `@@`
	Right []AndExprRight `@@*`
}

AndExpr ...

func (*AndExpr) String

func (e *AndExpr) String() string

type AndExprRight

type AndExprRight struct {
	Op    Operator `@"and"`
	Right *Compare `@@`
}

AndExprRight ...

func (AndExprRight) String

func (e AndExprRight) String() string

type Arith

type Arith struct {
	Left  *Factor      `@@`
	Right []ArithRight `@@*`
}

Arith ...

func (*Arith) String

func (e *Arith) String() string

type ArithRight

type ArithRight struct {
	Op    Operator `@("+" | "-")`
	Right *Factor  `@@`
}

ArithRight ...

func (ArithRight) String

func (e ArithRight) String() string

type Array

type Array struct {
	Query *Query `"[" @@? "]"`
}

Array ...

func (*Array) String

func (e *Array) String() string

type Bind

type Bind struct {
	Patterns []*Pattern `"as" @@ ("?//" @@)*`
	Body     *Query     `"|" @@`
}

Bind ...

func (*Bind) String

func (e *Bind) String() string

type Comma

type Comma struct {
	Filters []*Filter `@@ ("," @@)*`
}

Comma ...

func (*Comma) String

func (e *Comma) String() string

type Compare

type Compare struct {
	Left  *Arith        `@@`
	Right *CompareRight `@@?`
}

Compare ...

func (*Compare) String

func (e *Compare) String() string

type CompareRight

type CompareRight struct {
	Op    Operator `@CompareOp`
	Right *Arith   `@@`
}

CompareRight ...

func (*CompareRight) String

func (e *CompareRight) String() string

type Expr

type Expr struct {
	Logic    *Logic   `( @@`
	If       *If      `| @@`
	Try      *Try     `| @@`
	Reduce   *Reduce  `| @@`
	Foreach  *Foreach `| @@ )`
	UpdateOp Operator `( ( @UpdateOp | @UpdateAltOp )`
	Update   *Alt     `  @@`
	Bind     *Bind    `| @@ )?`
	Label    *Label   `| @@`
}

Expr ...

func (*Expr) String

func (e *Expr) String() string

type Factor

type Factor struct {
	Left  *Term         `@@`
	Right []FactorRight `@@*`
}

Factor ...

func (*Factor) String

func (e *Factor) String() string

type FactorRight

type FactorRight struct {
	Op    Operator `@("*" | "/" | "%")`
	Right *Term    `@@`
}

FactorRight ...

func (FactorRight) String

func (e FactorRight) String() string

type Filter

type Filter struct {
	FuncDefs []*FuncDef `@@*`
	Alt      *Alt       `@@`
}

Filter ...

func (*Filter) String

func (e *Filter) String() string

type Foreach

type Foreach struct {
	Term    *Term    `"foreach" @@`
	Pattern *Pattern `"as" @@`
	Start   *Query   `"(" @@`
	Update  *Query   `";" @@`
	Extract *Query   `(";" @@)? ")"`
}

Foreach ...

func (*Foreach) String

func (e *Foreach) String() string

type Func

type Func struct {
	Name string   `@Ident`
	Args []*Query `( "(" @@ (";" @@)* ")" )?`
}

Func ...

func (*Func) String

func (e *Func) String() string

type FuncDef

type FuncDef struct {
	Name string   `"def" @Ident`
	Args []string `("(" @Ident (";" @Ident)* ")")? ":"`
	Body *Query   `@@ ";"`
}

FuncDef ...

func (*FuncDef) String

func (e *FuncDef) String() string

type If

type If struct {
	Cond *Query   `"if" @@`
	Then *Query   `"then" @@`
	Elif []IfElif `@@*`
	Else *Query   `("else" @@)? "end"`
}

If ...

func (*If) String

func (e *If) String() string

type IfElif

type IfElif struct {
	Cond *Query `"elif" @@`
	Then *Query `"then" @@`
}

IfElif ...

func (*IfElif) String

func (e *IfElif) String() string

type Index

type Index struct {
	Name    string `"." ( @Ident`
	Str     string `| @String`
	Start   *Query `| "[" ( @@`
	IsSlice bool   `( @":"`
	End     *Query `@@? )? | ":" @@ ) "]" )`
}

Index ...

func (*Index) String

func (e *Index) String() string

type Iter

type Iter interface {
	Next() (interface{}, bool)
}

Iter ...

type Label

type Label struct {
	Ident string `"label" @Ident`
	Body  *Query `"|" @@`
}

Label ...

func (*Label) String

func (e *Label) String() string

type Logic

type Logic struct {
	Left  *AndExpr     `@@`
	Right []LogicRight `@@*`
}

Logic ...

func (*Logic) String

func (e *Logic) String() string

type LogicRight

type LogicRight struct {
	Op    Operator `@"or"`
	Right *AndExpr `@@`
}

LogicRight ...

func (LogicRight) String

func (e LogicRight) String() string

type Object

type Object struct {
	KeyVals []ObjectKeyVal `"{" (@@ ("," @@)*)? "}"`
}

Object ...

func (*Object) String

func (e *Object) String() string

type ObjectKeyVal

type ObjectKeyVal struct {
	Key           string  `( ( ( @Ident | @Keyword )`
	KeyString     string  `  | @String )`
	Query         *Query  `| "(" @@ ")" ) ":"`
	Val           *Expr   `@@`
	KeyOnly       *string `| @Ident`
	KeyOnlyString string  `| @String`
}

ObjectKeyVal ...

func (*ObjectKeyVal) String

func (e *ObjectKeyVal) String() string

type Operator

type Operator int

Operator ...

const (
	OpAdd Operator = iota
	OpSub
	OpMul
	OpDiv
	OpMod
	OpEq
	OpNe
	OpGt
	OpLt
	OpGe
	OpLe
	OpAnd
	OpOr
	OpAlt
	OpAssign
	OpModify
	OpUpdateAdd
	OpUpdateSub
	OpUpdateMul
	OpUpdateDiv
	OpUpdateMod
	OpUpdateAlt
)

Operators ...

func (*Operator) Capture

func (op *Operator) Capture(s []string) error

Capture implements participle.Capture.

func (Operator) GoString

func (op Operator) GoString() string

GoString implements GoStringer.

func (Operator) String

func (op Operator) String() string

String implements Stringer.

type Pattern

type Pattern struct {
	Name   string          `  @Ident`
	Array  []*Pattern      `| "[" @@ ("," @@)* "]"`
	Object []PatternObject `| "{" @@ ("," @@)* "}"`
}

Pattern ...

func (*Pattern) String

func (e *Pattern) String() string

type PatternObject

type PatternObject struct {
	Key       string   `( ( @Ident | @Keyword )`
	KeyString string   `  | @String`
	Query     *Query   `  | "(" @@ ")" ) ":"`
	Val       *Pattern `@@`
	KeyOnly   string   `| @Ident`
}

PatternObject ...

func (*PatternObject) String

func (e *PatternObject) String() string

type Query

type Query struct {
	Commas []*Comma `@@ ("|" @@)*`
}

Query ...

func Parse

func Parse(src string) (*Query, error)

Parse parses a query.

func (*Query) Run

func (e *Query) Run(v interface{}) Iter

Run query.

func (*Query) String

func (e *Query) String() string

type Reduce

type Reduce struct {
	Term    *Term    `"reduce" @@`
	Pattern *Pattern `"as" @@`
	Start   *Query   `"(" @@`
	Update  *Query   `";" @@ ")"`
}

Reduce ...

func (*Reduce) String

func (e *Reduce) String() string

type Suffix

type Suffix struct {
	Index       *Index       `  @@`
	SuffixIndex *SuffixIndex `| @@`
	Iter        bool         `| @("[" "]")`
	Optional    bool         `| @"?"`
}

Suffix ...

func (*Suffix) String

func (e *Suffix) String() string

type SuffixIndex

type SuffixIndex struct {
	Start   *Query `"[" ( @@`
	IsSlice bool   `( @":"`
	End     *Query `@@? )? | ":" @@ ) "]"`
}

SuffixIndex ...

func (*SuffixIndex) String

func (e *SuffixIndex) String() string

type Term

type Term struct {
	Index      *Index    `( @@`
	Identity   bool      `| @"."`
	Recurse    bool      `| @".."`
	Func       *Func     `| @@`
	Object     *Object   `| @@`
	Array      *Array    `| @@`
	Number     string    `| @Number`
	Unary      *Unary    `| @@`
	Str        string    `| @String`
	RawStr     string    `| @" "` // never matches, used in compiler
	Null       bool      `| @"null"`
	True       bool      `| @"true"`
	False      bool      `| @"false"`
	Break      string    `| "break" @Ident`
	Query      *Query    `| "(" @@ ")" )`
	SuffixList []*Suffix `@@*`
}

Term ...

func (*Term) String

func (e *Term) String() string

type Try

type Try struct {
	Body  *Query `"try" @@`
	Catch *Query `("catch" @@)?`
}

Try ...

func (*Try) String

func (e *Try) String() string

type Unary

type Unary struct {
	Op   Operator `@("+" | "-")`
	Term *Term    `@@`
}

Unary ...

func (*Unary) String

func (e *Unary) String() string

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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