Show / Hide Table of Contents

Class JsonTransformer

Provides functionality for applying a JMESPath expression to transform a JSON document into another JSON document

Inheritance
System.Object
JsonTransformer
Inherited Members
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.ToString()
Namespace: JsonCons.JmesPath
Assembly: JsonCons.JmesPath.dll
Syntax
public sealed class JsonTransformer
Examples

The following example shows how to apply a JMESPath expression to transform a JSON document into another JSON document.

using System;
using System.Text.Json;
using JsonCons.JmesPath;

public class Example
{
    public static void Main()
    {
   string jsonString = @"
{
""people"": [
{
  ""age"": 20,
  ""other"": ""foo"",
  ""name"": ""Bob""
},
{
  ""age"": 25,
  ""other"": ""bar"",
  ""name"": ""Fred""
},
{
 ""age"": 30,
 ""other"": ""baz"",
 ""name"": ""George""
}
]
}
   ";

   using JsonDocument doc = JsonDocument.Parse(jsonString);

   var transformer = JsonTransformer.Parse("people[?age > `20`].[name, age]");

   using JsonDocument result = transformer.Transform(doc.RootElement);

   var serializerOptions = new JsonSerializerOptions() {WriteIndented = true};
   Console.WriteLine(JsonSerializer.Serialize(result.RootElement, serializerOptions));
}

Output:

[
  [
    "Fred",
    25
  ],
  [
    "George",
    30
  ]
]

Methods

| Improve this Doc View Source

Parse(String)

Parses a JMESPath string into a JsonTransformer, for "parse once, use many times". A JsonTransformer instance is thread safe and has no mutable state.

Declaration
public static JsonTransformer Parse(string jmesPath)
Parameters
Type Name Description
System.String jmesPath

A JMESPath string.

Returns
Type Description
JsonTransformer

A JsonTransformer.

Exceptions
Type Condition
JmesPathParseException

The jmesPath parameter is not a valid JMESPath expression.

System.ArgumentNullException

The jmesPath is null.

| Improve this Doc View Source

Transform(JsonElement)

Applies a JMESPath expression to a JSON document to transform it into another Json document.

Declaration
public JsonDocument Transform(JsonElement doc)
Parameters
Type Name Description
System.Text.Json.JsonElement doc

The provided JSON document.

Returns
Type Description
System.Text.Json.JsonDocument

The transformed JSON document. If a type error is detected in a function call, a JSON null value is returned.

Remarks

It is the users responsibilty to properly Dispose the returned System.Text.Json.JsonDocument value

| Improve this Doc View Source

Transform(JsonElement, String)

Applies a JMESPath expression to a JSON document to transform it into another Json document. This method parses and applies the expression in one operation.

Declaration
public static JsonDocument Transform(JsonElement doc, string jmesPath)
Parameters
Type Name Description
System.Text.Json.JsonElement doc

The provided JSON document.

System.String jmesPath

A JMESPath string.

Returns
Type Description
System.Text.Json.JsonDocument

The transformed JSON document.

Remarks

It is the users responsibilty to properly Dispose the returned System.Text.Json.JsonDocument value

Exceptions
Type Condition
JmesPathParseException

The jmesPath parameter is not a valid JMESPath expression.

System.ArgumentNullException

The jmesPath is null.

  • Improve this Doc
  • View Source
In This Article
Back to top Generated by DocFX