Skip to content

JSON Extension Library

github-actions[bot] edited this page Nov 29, 2026 · 1 revision

JSON Extension Library

JsonLibrary is a Celly CelLibrary that adds json.parse(string) and json.stringify(dyn) directly inside CEL expressions.

This is especially useful for:

  • Inspecting embedded JSON strings inside webhooks, HTTP request bodies, or audit log records
  • Decoding JSON claims inside JWT payloads
  • Formatting computed CEL maps/lists into JSON strings for downstream templates or logs

Registration

Pass JsonLibrary.Instance (or a customized JsonLibrary.Create(...)) in CelEnvSettings.Libraries:

CelEnv env = CelEnv.Create(new CelEnvSettings
{
    Libraries = [JsonLibrary.Instance],
});

You can also pass it to JsonSchemaEnv.Create:

CelEnv env = JsonSchemaEnv.Create(schema, libraries: [JsonLibrary.Instance]);

Functions

json.parse(string) -> dyn

Parses a JSON string into a detached, lazy Celly.Json value (JsonElementObjectValue, JsonElementListValue, or primitive CelValue).

json.parse(webhook.rawBody).event == 'invoice.paid'
  • Uses CelJsonOptions (default: CelJsonOptions.Default) for number handling and property lookup.
  • If the input string is not valid JSON, returns a CEL ErrorValue ("json.parse failed: ...") rather than throwing an unhandled exception — allowing CEL's standard error-absorbing boolean logic (||, &&) to handle it safely.

json.stringify(dyn) -> string

Serializes any CEL value to a JSON string using CelJsonWriter:

json.stringify({'user': user.name, 'roles': user.roles})
  • Follows the canonical CEL-to-JSON mapping (see Serializing CEL Results).
  • Returns a CEL ErrorValue if the input cannot be serialized (e.g., an UnknownValue).

Custom Options

Configure custom CelJsonOptions, JsonDocumentOptions (e.g., allowing comments or trailing commas), or JsonWriterOptions (e.g., Indented = true):

CelLibrary customJsonLib = JsonLibrary.Create(
    options: new CelJsonOptions { NumberHandling = CelJsonNumberHandling.AlwaysDouble },
    documentOptions: new JsonDocumentOptions { AllowTrailingCommas = true },
    writerOptions: new JsonWriterOptions { Indented = true });

CelEnv env = CelEnv.Create(new CelEnvSettings
{
    Libraries = [customJsonLib],
});

Clone this wiki locally