Denomica.Text.Json

Extensions and utility functions for System.Text.Json.

View the Project on GitHub Denomica/Denomica.Text.Json

Convert to Dictionary

Sometimes it is more appropriate to handle data from a JSON document as a dictionary. For instance the merging functionality makes use of dictionary objects.

Converting a JsonDocument or JsonElement object into a dictionary is implemented in the ExtensionMethods class, in the ToDictionary methods.

The method requires that the JSON element being converted represents an object, as shown below.

JsonElement.ValueKind == JsonValueKind.Object

The method then enumerates the object’s properties adding each property as an item in the dictionary. The value of each property is handled differently depending on the type of value. The method supports all different types defined in the JsonValueKind enumeration.