gojq

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2019 License: MIT Imports: 13 Imported by: 472

README

gojq Travis Build Status

Yet another 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

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

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 added in v0.2.0

type Alt struct {
	Left  *Expr `@@`
	Right []struct {
		Op    Operator `@"//"`
		Right *Expr    `@@`
	} `@@*`
}

Alt ...

type AndExpr added in v0.1.0

type AndExpr struct {
	Left  *Compare `@@`
	Right []struct {
		Op    Operator `@"and"`
		Right *Compare `@@`
	} `@@*`
}

AndExpr ...

type Arith added in v0.1.0

type Arith struct {
	Left  *Factor `( @@`
	Right []struct {
		Op    Operator `@("+" | "-")`
		Right *Factor  `@@`
	} `@@* )`
}

Arith ...

type Array

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

Array ...

type Comma

type Comma struct {
	Alts []*Alt `@@ ("," @@)*`
}

Comma ...

type Compare added in v0.1.0

type Compare struct {
	Left  *Arith `@@`
	Right *struct {
		Op    Operator `@CompareOp`
		Right *Arith   `@@`
	} `@@?`
}

Compare ...

type Expr added in v0.1.0

type Expr struct {
	Logic   *Logic   `( @@`
	If      *If      `| @@`
	Try     *Try     `| @@`
	Reduce  *Reduce  `| @@`
	Foreach *Foreach `| @@ )`
	Bind    *struct {
		Pattern *Pattern `"as" @@`
		Body    *Pipe    `"|" @@`
	} `@@?`
	Label *Label `| @@`
}

Expr ...

type Factor added in v0.1.0

type Factor struct {
	Left  *Term `@@`
	Right []struct {
		Op    Operator `@("*" | "/" | "%")`
		Right *Term    `@@`
	} `@@*`
}

Factor ...

type Foreach added in v0.3.0

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

Foreach ...

type Func added in v0.1.0

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

Func ...

type FuncDef added in v0.1.0

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

FuncDef ...

type If added in v0.1.0

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

If ...

type Index added in v0.2.0

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

Index ...

type Label added in v0.3.0

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

Label ...

type Logic added in v0.1.0

type Logic struct {
	Left  *AndExpr `@@`
	Right []struct {
		Op    Operator `@"or"`
		Right *AndExpr `@@`
	} `@@*`
}

Logic ...

type Object

type Object struct {
	KeyVals []struct {
		Key           string  `( ( ( @Ident | @Keyword )`
		KeyString     *string `  | @String )`
		Pipe          *Pipe   `| "(" @@ ")" ) ":"`
		Val           *Expr   `@@`
		KeyOnly       *string `| @Ident`
		KeyOnlyString *string `| @String`
	} `"{" (@@ ("," @@)*)? "}"`
}

Object ...

type Operator added in v0.1.0

type Operator int

Operator ...

const (
	OpAdd Operator = iota
	OpSub
	OpMul
	OpDiv
	OpMod
	OpEq
	OpNe
	OpGt
	OpLt
	OpGe
	OpLe
	OpAnd
	OpOr
	OpAlt
)

Operators ...

func (*Operator) Capture added in v0.1.0

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

Capture implements participle.Capture.

func (Operator) Eval added in v0.1.0

func (op Operator) Eval(l, r interface{}) interface{}

Eval the expression.

func (Operator) String added in v0.1.0

func (op Operator) String() string

String implements Stringer.

type Pattern added in v0.3.0

type Pattern struct {
	Name   string     `( @Ident`
	Array  []*Pattern `| "[" @@ ("," @@)* "]"`
	Object []struct {
		Key       string   `( ( @Ident | @Keyword )`
		KeyString string   `| @String ) ":"`
		Val       *Pattern `@@`
		KeyOnly   string   `| @Ident`
	} `| "{" @@ ("," @@)* "}" )`
}

Pattern ...

type Pipe

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

Pipe ...

type Query

type Query struct {
	FuncDefs []*FuncDef `@@*`
	Pipe     *Pipe      `@@?`
}

Query ...

func Parse added in v0.1.0

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

Parse parses a query.

func (*Query) Run added in v0.1.0

func (q *Query) Run(v interface{}) <-chan interface{}

Run query.

type Reduce added in v0.3.0

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

Reduce ...

type Suffix

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

Suffix ...

type SuffixIndex added in v0.2.0

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

SuffixIndex ...

type Term

type Term struct {
	Index    *Index   `( @@`
	Identity bool     `| @"."`
	Recurse  bool     `| @".."`
	Func     *Func    `| @@`
	Object   *Object  `| @@`
	Array    *Array   `| @@`
	Number   *float64 `| @Number`
	Unary    *struct {
		Op   Operator `@("+" | "-")`
		Term *Term    `@@`
	} `| @@`
	String     *string   `| @String`
	Null       bool      `| @"null"`
	True       bool      `| @"true"`
	False      bool      `| @"false"`
	Break      string    `| "break" @Ident`
	Pipe       *Pipe     `| "(" @@ ")" )`
	SuffixList []*Suffix `@@*`
}

Term ...

type Try added in v0.2.0

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

Try ...

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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