---
title: "Insert JSON into a .NET prompt template"
url: https://recursiveai.org/onellm/cookbook/insert-json-data-into-prompt-template/
description: "Insert JSON into a .NET prompt template with OneLLM’s toJson helper. Render typed C# input locally without a model call, API key, or hand-built JSON."
version: "1.0.1"
---

# Insert JSON into a .NET prompt template

**OneLLM** is a free, open-source LLM and agent library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneLlm) · [GitHub](https://github.com/recursive-ai-oss/onellm-csharp) · [Docs](https://recursiveai.org/onellm/docs/) · [Download example (.zip)](https://recursiveai.org/onellm/cookbook/insert-json-data-into-prompt-template/insert-json-data-into-prompt-template.zip)

Render the prompt locally from a C# object.

```shell
dotnet add package RecursiveAI.OneLlm
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using System.Text.Json;
using OneLlm.Templating;

var order = new { item = "Desk lamp", qty = 2 };
var data = JsonSerializer
    .SerializeToElement(order);
Console.WriteLine(Template.Render(
    "Summarize: {{ . | toJson }}", data));
```

Templates use Go `text/template` syntax; `toJson` is a OneLLM extension. The `.` in the template is the whole input value. `| toJson` turns that value into JSON text at that spot. Rendering makes no model call and doesn’t need an account. If the template is malformed, `Template.Render` throws `TemplateException` with its location.

Unzip [the example](https://recursiveai.org/onellm/cookbook/insert-json-data-into-prompt-template/insert-json-data-into-prompt-template.zip), then `dotnet run`.

```text
Summarize: {"item":"Desk lamp","qty":2}
```

## Variations

**Pretty-print the JSON on its own lines.** Add this after the snippet at the top. `toPrettyJson` indents by two spaces instead of putting the object on a single line.

```csharp
Console.WriteLine(Template.Render(
    "Order data:\n{{ . | toPrettyJson }}", data));
```

```text
Order data:
{
  "item": "Desk lamp",
  "qty": 2
}
```

**Insert just one value.** Pick the field in the template before calling `toJson`. Here the quantity doesn’t appear in the rendered prompt.

```csharp
Console.WriteLine(Template.Render(
    "Product: {{ .item | toJson }}", data));
```

```text
Product: "Desk lamp"
```

**Start with existing JSON text.** Parse it first so `toJson` receives an object instead of a string full of escaped quotes. Add this after the snippet at the top.

```csharp
using var doc = JsonDocument.Parse("""
    {"item":"Desk lamp","qty":2}
    """);
Console.WriteLine(Template.Render(
    "Summarize: {{ . | toJson }}",
    doc.RootElement));
```

```text
Summarize: {"item":"Desk lamp","qty":2}
```

## Related recipes

- [Render prompt templates with variables in C#](https://recursiveai.org/onellm/cookbook/render-prompt-template/)
- [Fail a prompt template on a missing key](https://recursiveai.org/onellm/cookbook/fail-prompt-template-on-missing-key/)
- [Read structured output into a C# record](https://recursiveai.org/onellm/cookbook/structured-output-to-a-csharp-record/)

## The short answer

Convert your C# input to a `JsonElement`, pass it to `Template.Render`, and pipe the value you want to insert through `toJson`. Use `toPrettyJson` if you want to inspect a nested payload before sending it. Both helpers render JSON text inside a prompt; they don’t send the prompt or ask a model to generate JSON.

## How can I use JSON to create prompts?

Write the task in plain language and use the JSON for the data the task refers to. In the example, `Summarize:` is the instruction; the object after it contains the order details. Keep the object typed until you render it so the number stays a number. Passing a string that already contains JSON to `toJson` gives you a JSON string with escaped quotes, not an embedded object. If you already have JSON text, parse it into a `JsonElement` first to work with its fields.

OneLLM’s [template functions](https://recursiveai.org/onellm/docs/templating/#onellm-only-extensions) sort object keys and don’t apply HTML escaping. They’re extensions to Go’s `text/template` syntax, so a host that only implements the standard Go functions won’t know what `toJson` means. When you need to take a template between hosts, check which functions the destination supports.

## How do you get JSON back from a prompt?

Inserting JSON and receiving JSON solve different problems. The result here is a string with an instruction followed by JSON; the whole string isn’t a JSON document or a model response. If your next step is to send that prompt as an API message, give the client the rendered string and let it construct the request body. Don’t paste the object into the transport body in place of the message.

If your application needs a JSON reply, use a response format supported by the model and parse the returned content separately. The [structured output recipe](https://recursiveai.org/onellm/cookbook/structured-output-to-a-csharp-record/) shows that step with a C# record. You can test your prompt rendering without it: check that a change to the input object changes the intended JSON field, while the instruction stays the same.
