| Development of this project is entirely funded by the community. Consider donating to support! |
Made in 🇺🇦 Ukraine — despite Russia's best efforts. Using this project or its source code comes with the implicit agreement that you condemn Russia's war of aggression against Ukraine, recognize Russia as a terrorist state, and support Ukraine's territorial integrity.
QuickJson is a very basic, lightweight JSON parser.
- 📦 NuGet:
dotnet add package QuickJson
Tip
You can use Binternal to internalize this library if you prefer to avoid taking an external dependency.
To parse a JSON string, call Json.TryParse(...) or Json.Parse(...):
// This returns null on invalid JSON
var json = Json.TryParse(
"""
{
"foo": [
69,
true,
"bar"
]
}
"""
);
// This throws on invalid JSON
var json = Json.Parse("...");To retrieve a nested node in the parsed JSON, use [Try]GetChild(...):
// May return null if the property doesn't exist
// or if the referenced node is not an object
var foo = json.TryGetChild("foo");
// May return null if the child doesn't exist
// or if the referenced node is not an array
var child1 = foo?.TryGetChild(0);
var child2 = foo?.TryGetChild(1);
var child3 = foo?.TryGetChild(2);Alternatively, you can also enumerate all object properties using EnumerateProperties() or all array children using EnumerateChildren():
// Returns an empty enumerator if the referenced node is not an object
foreach (var prop in json.EnumerateProperties())
{
var name = prop.Name; // string
var value = prop.Value; // JsonNode
// ...
}
// Returns an empty enumerator if the referenced node is not an array
foreach (var child in json.EnumerateChildren())
{
// ...
}In order to extract values from nodes, use [Try]GetNumber(), [Try]GetBool(), or [Try]GetString():
// May return null if the node contains a value of a different kind
var value1 = child1?.TryGetNumber(); // double
var value2 = child2?.TryGetBool(); // bool
var value3 = child3?.TryGetString(); // string