typestar

JObject in C#

Newtonsoft's dynamic view of JSON: parse, read, patch, write.

using Newtonsoft.Json.Linq;

var doc = JObject.Parse("""
{"repo": "typestar", "stars": 42, "topics": ["typing", "cli"]}
""");

Console.WriteLine(doc["repo"]);
Console.WriteLine(doc["topics"][0]);

// a JObject is mutable: patch it and write it back out
doc["stars"] = 43;
doc["forked"] = false;
Console.WriteLine(doc.ToString(Newtonsoft.Json.Formatting.None));

How it works

  1. JObject.Parse accepts the document as it is.
  2. Indexers read fields and array elements without any classes.
  3. Assigning to an index adds or updates; ToString writes it back.

Keywords and builtins used here

The run, in numbers

Lines
13
Characters to type
360
Tokens
87
Three-star pace
85 tpm

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

Type this snippet

Step 1 of 3 in JSON with Newtonsoft, step 1 of 16 in The open ecosystem.

Next →