Categorygithub.com/danielgtaylor/go-jmespath-plus
modulepackage
0.0.0-20200228063638-e0b6f132acba
Repository: https://github.com/danielgtaylor/go-jmespath-plus.git
Documentation: pkg.go.dev

# README

go-jmespath-plus - A JMESPath implementation in Go

CI

See http://jmespath.org for more info.

Enhancements

Recursive Descent Operator

A new .. recursive descent operator is supported. It will recursively traverse all objects and arrays to create a new projection. For example, given:

{
  "foo": {
    "bar": 1,
    "baz": [
      {
        "bar": 2
      },
      {
        "bar": 3
      }
    ]
  }
}

Then you can see the difference between the a wildcard and recursive descent:

# Wildcard key, selects only the first `bar`.
$ jpgo '*.bar' <input
[1]

# Recursive descent, selects *all* the `bar` entries.
$ jpgo '..bar' <input
[1, 2, 3]

Multi-select Hash Shorthand

Selecting a single key from an object is now possible with a property shorthand syntax borrowed from modern Javascript. Given:

{
  "foo": {
    "bar": 1,
    "baz": [true, false]
  }
}

Then you can select via the shorthand:

# Shorthand example.
$ jpgo 'foo.{bar}' <input
{"bar": 1}

# Mixing shorthand and long-form.
$ jpgo 'foo.{bar, first_baz: baz[0]}' <input
{"bar": 1, "first_baz": true}

Group By Function

The group_by function from jq is borrowed to generate a list of grouped objects based on the result of an expression executed on each item in the incoming array. The output is sorted in ascending order.

group_by(array $elements, expression->number|expression->string field)

Given:

{
  "foo": [
    {
      "id": 1,
      "type": "red"
    },
    {
      "id": 2,
      "type": "blue"
    },
    {
      "id": 3,
      "type": "red"
    }
  ]
}

Then you can group the items by their type:

# Group the inputs by the type of item.
$ jpgo 'group_by(foo, &type)' <input

The result:

[
  [
    {
      "id": 2,
      "type": "blue"
    }
  ],
  [
    {
      "id": 1,
      "type": "red"
    },
    {
      "id": 3,
      "type": "red"
    }
  ]
]

Pivot Function

The pivot function is a convenience wrapper around group_by(...) that also pivots the data to return an object where the keys are the grouping value and the values are the groups of objects with the given projection expression applied to each object.

pivot(array $elements, expression->number|expression->string field, expression projection)

Given:

{
  "foo": [
    {
      "id": 1,
      "type": "red"
    },
    {
      "id": 2,
      "type": "blue"
    },
    {
      "id": 3,
      "type": "red"
    }
  ]
}

Then you can pivot the items by their type:

# Pivot items by their type
$ jpgo 'pivot(foo, &type, &id)' <input

The result:

{
  "blue": [2],
  "red": [1, 3]
}

You can also use the identity to keep the full objects:

# Pivot items by their type and keep each original item.
$ jpgo 'pivot(foo, &type, &@)' <input

The result:

{
  "blue": [
    {
      "id": 2,
      "type": "blue"
    }
  ],
  "red": [
    {
      "id": 1,
      "type": "red"
    },
    {
      "id": 3,
      "type": "red"
    }
  ]
}

# Packages

No description provided by the author
No description provided by the author

# Functions

Compile parses a JMESPath expression and returns, if successful, a JMESPath object that can be used to match against data.
MustCompile is like Compile but panics if the expression cannot be parsed.
NewLexer creates a new JMESPath lexer.
NewParser creates a new JMESPath parser.
Search evaluates a JMESPath expression against input data and returns the result.

# Constants

go:generate stringer -type astNodeType.
go:generate stringer -type astNodeType.
go:generate stringer -type astNodeType.
go:generate stringer -type astNodeType.
go:generate stringer -type astNodeType.
go:generate stringer -type astNodeType.
go:generate stringer -type astNodeType.
go:generate stringer -type astNodeType.
go:generate stringer -type astNodeType.
go:generate stringer -type astNodeType.
go:generate stringer -type astNodeType.
go:generate stringer -type astNodeType.
go:generate stringer -type astNodeType.
go:generate stringer -type astNodeType.
go:generate stringer -type astNodeType.
go:generate stringer -type astNodeType.
go:generate stringer -type astNodeType.
go:generate stringer -type astNodeType.
go:generate stringer -type astNodeType.
go:generate stringer -type astNodeType.
go:generate stringer -type astNodeType.
go:generate stringer -type astNodeType.
go:generate stringer -type astNodeType.
go:generate stringer -type astNodeType.

# Structs

ASTNode represents the abstract syntax tree of a JMESPath expression.
JMESPath is the epresentation of a compiled JMES path query.
Lexer contains information about the expression being tokenized.
Parser holds state about the current expression being parsed.
SyntaxError is the main error used whenever a lexing or parsing error occurs.