jqlite for .NET

JQLite is the .NET reference implementation of jqlite, the portable profile of the jq language. It follows the same spec and passes the same conformance corpus as the other implementations; the Go, Java, Python, Rust and TypeScript engines are ported from it file by file (see the porting guide).

Programs read and produce System.Text.Json values: you give a JsonElement and get JsonElements back.

Requirements

  • .NET 8 or later: the package targets net8.0.
  • No package dependencies besides the framework.
  • The library is marked trimmable and Native AOT compatible: it uses no reflection and no JSON serializer.

Get the package

The package is JQLite, released to nuget.org:

dotnet add package JQLite

Builds of every branch and pull request are published to the CI feed https://pkg.kzu.app/index.json, with versions such as 42.42.42-main.123. To build from source instead, clone devlooped/jqlite and reference the project:

dotnet add reference path/to/jqlite/dotnet/JQLite/JQLite.csproj

The package is distributed under the OSMF EULA.

A first program

using System.Text.Json;
using JQLite;

using var doc = JsonDocument.Parse(
    """
    {
      "users": [
        { "name": "Alice", "age": 30 },
        { "name": "Bob", "age": 17 },
        { "name": "Carol", "age": 42 }
      ]
    }
    """);

IReadOnlyList<JsonElement> results = Jq.Evaluate(
    ".users[] | select(.age >= 18) | {name, adult: true}",
    doc.RootElement);

foreach (var result in results)
    Console.WriteLine(result.GetRawText());
{"name":"Alice","adult":true}
{"name":"Carol","adult":true}

Jq is the entry point: Jq.Evaluate parses the program, runs it on one input value and returns every result. A program can produce zero, one or many results; here .users[] produces one value per user and select drops the minors.

GetRawText() gives the JSON text of a result. ToString(), which Console.WriteLine(result) uses, gives the same text for numbers, arrays and objects, but a string without its quotes.

In this section

Page What it covers
Evaluating One-shot evaluation, parsing once, streaming results, threads
Values How JSON values go in and come out: numbers, strings, key order
Variables Passing $name variables from your code
Options Environment, clock and time zone, depth limit, debug and stderr
Modules import, include, data modules and resolvers
Errors The exceptions, and catching errors inside a program
JSON Lines Running a program on each value of a stream
Upgrading from 1.x What changed since JQSharp 1.x

The API reference is generated from the XML documentation: see the API. For the language itself, start with the jqlite language.