jdevillard / JmesPath.Net

A fully compliant implementation of JMESPATH for .NetCore

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

JMESPath.Net

A fully compliant implementation of JMESPath for .Net Core.

Build status

Getting started

Using the parser

JMESPath.Net uses Newtonsoft.Json to handle JSON and comes with a simple to use parser:

using DevLab.JmesPath;

const string input = @"{ \"foo\": \"bar\" }";
const string expression = "foo";

var jmes = new JmesPath();
var result = jmes.Transform(input, expression);

The JmesPath.Transform method accepts and produces well formed JSON constructs (object, array or string, boolean, number and null values). In the example above, the result is a JSON string token, including the quotes.

using Newtonsoft.Json.Linq;

System.Diagnostics.Debug.Assert(result == "\"bar\"");

var token = JToken.Parse(result);
var text = token.ToString();

System.Diagnostics.Debug.Assert(text == "bar");

Exploiting the AST

The previous example return the result of the JMESPath evaluation in a single function call. The JMESPath.Net parser, however, create an abstract syntax tree with a root JmesPathExpression object used for evaluating JMESPath expression against a JSON document.

If you want to retrieve the AST before performing evaluations, use the following code:

var jp = new JmesPath();
var ast = jp.Parse(expression);

System.Diagnostics.Debug.Assert(ast is JmesPathExpression);

Here is the inheritance hierarchy of JmesPathExpression objects:

classDiagram
	class JmesPathExpression
	JmesPathExpression: +bool IsExpressionType()
    JmesPathExpression <|-- JmesPathSimpleExpression
    JmesPathExpression <|-- JmesPathCompoundExpression
    JmesPathExpression <|-- JmesPathProjection
    JmesPathExpression <|-- JmesPathFunctionExpression
    JmesPathExpression <|-- JmesPathLetExpression
    JmesPathExpression <|-- JmesPathVariableReference

	JmesPathFunctionExpression: +string Name
	JmesPathFunctionExpression: +JmesPathExpression[] Arguments

Leaf Expressions

classDiagram
    JmesPathExpression <|-- JmesPathCurrentNode
    JmesPathExpression <|-- JmesPathIdentifier
    JmesPathExpression <|-- JmesPathIndex
    JmesPathExpression <|-- JmesPathLiteral
    JmesPathExpression <|-- JmesPathMultiSelectHash
    JmesPathExpression <|-- JmesPathMultiSelectList
    JmesPathExpression <|-- JmesPathRawString

	JmesPathMultiSelectList: +JmesPathExpression[] Expressions
	JmesPathMultiSelectHash: +IDictionary~string, JmesPathExpression~ Dictionary
	JmesPathIdentifier: +string Name
	JmesPathIndex: +int Index
	JmesPathLiteral: +JToken Value
	JmesPathRawString: + string Value

Simple Expressions

A simple expression encapsulates a single instance of another expression named Expression.

classDiagram
	class JmesPathSimpleExpression
	JmesPathSimpleExpression: +JmesPathExpression Expression

	JmesPathSimpleExpression <|-- JmesPathNotExpression
	JmesPathSimpleExpression <|-- JmesPathParenExpression

Compound Expressions

Compound expressions are a combination of two expressions, named Left and Right.

classDiagram
	class JmesPathCompoundExpression
	JmesPathCompoundExpression: +JmesPathExpression Left
	JmesPathCompoundExpression: +JmesPathExpression Right

	JmesPathCompoundExpression <|-- JmesPathAndExpression
	JmesPathCompoundExpression <|-- JmesPathComparison
	JmesPathCompoundExpression <|-- JmesPathIndexExpression
	JmesPathCompoundExpression <|-- JmesPathOrExpression
	JmesPathCompoundExpression <|-- JmesPathPipeExpression
	JmesPathCompoundExpression <|-- JmesPathSubExpression
classDiagram
	JmesPathProjection <|-- JmesPathFilterProjection
	JmesPathProjection <|-- JmesPathFlattenProjection
	JmesPathProjection <|-- JmesPathHashWildcardProjection
	JmesPathProjection <|-- JmesPathListWildcardProjection
	JmesPathProjection <|-- JmesPathSliceProjection

	JmesPathFilterProjection: +JmesPathExpression Expression
	JmesPathSliceProjection: +int? Start
	JmesPathSliceProjection: +int? Stop
	JmesPathSliceProjection: +int? Step
classDiagram
	JmesPathComparison <|-- JmesPathEqualOperator
	JmesPathComparison <|-- JmesPathOrderingComparison
	JmesPathComparison <|-- JmesPathNotEqualOperator
classDiagram
	JmesPathOrderingComparison <|-- JmesPathGreaterThanOperator
	JmesPathOrderingComparison <|-- JmesPathGreaterThanOrEqualOperator
	JmesPathOrderingComparison <|-- JmesPathLessThanOperator
	JmesPathOrderingComparison <|-- JmesPathLessThanOrEqualOperator

About

A fully compliant implementation of JMESPATH for .NetCore

License:Apache License 2.0


Languages

Language:C# 96.1%Language:Yacc 1.9%Language:PowerShell 1.2%Language:Lex 0.8%