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
JObject.Parseaccepts the document as it is.- Indexers read fields and array elements without any classes.
- Assigning to an index adds or updates;
ToStringwrites it back.
Keywords and builtins used here
falseusingvar
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.
Step 1 of 3 in JSON with Newtonsoft, step 1 of 16 in The open ecosystem.