Recursive AI

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.

Render the prompt locally from a C# object.

Terminal
dotnet add package RecursiveAI.OneLlm

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
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, 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.

C#
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.

C#
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.

C#
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}

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 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 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.

All onellm recipes