typestar

JsonNode in C#

JSON you explore rather than map onto a type.

using System.Text.Json.Nodes;

var doc = JsonNode.Parse("""
{"user": {"name": "ada", "roles": ["admin", "dev"]}}
""");

// nodes are for JSON you explore rather than map onto a type
Console.WriteLine((string)doc["user"]["name"]);
Console.WriteLine(doc["user"]["roles"][1]);

doc["user"]["active"] = true;
Console.WriteLine(doc.ToJsonString());

How it works

  1. JsonNode.Parse builds a navigable tree from text.
  2. Indexers walk objects and arrays; casts read the leaves.
  3. Nodes are mutable: patch a field and serialize back out.

Keywords and builtins used here

The run, in numbers

Lines
12
Characters to type
343
Tokens
85
Three-star pace
85 tpm

At the three-star pace of 85 tokens a minute, this run takes about 60 seconds.

Type this snippet

Step 3 of 3 in JSON, step 6 of 17 in The .NET library.

← Previous Next →