Table of Contents

JsonDataReaderSchema

Defines the schema for the JsonDataReader by specifying the name of the JSON properties to read, the name of the columns that each property maps to, and the data type for each property.


Properties

Name Type Description
OnResolveType Func<string, Type> A callback that gets the name of the JSON property as input and returns the data type. (See example below).

Methods

Name Description
MapJsonProperty(string, string, Type) Maps a JSON property to a column and specifies the data type. (See example below).

Example - Define schema

The following example shows how to configure the schema JsonDataReader should use. Note that the lower case JSON properties are mapped to Pascal-cased column names, and that the nested properties under address are mapped to City and Street respectively.

var json = """
{
    "name": "John",
    "age": 30,
    "address": {
        "city": "New York",
        "street": "Broadway"
    }
}
""";

var reader = new JsonDataReader(json);
reader.Schema
    .MapJsonProperty("name", "Name", typeof(string))
    .MapJsonProperty("age", "Age", typeof(int))
    .MapJsonProperty("address.city", "City", typeof(string))
    .MapJsonProperty("address.street", "Street", typeof(string));

DataTable table = reader.ToDataTable();

Example - Using the OnResolveType callback

The example below shows how to override how JsonDataReader infers the data type of the weight property by specifying the OnResolveType callback.


var json = """
    {
    "name": "John",
    "age": 30,
    "city": "New York",
    "isStudent": false,
    "dateOfBirth": "1990-01-01T00:00:00",
    "weight": 70
    }
    """;

var reader = new JsonDataReader(json);
reader.Schema.OnResolveType = (propertyName) =>
{
    return propertyName switch
    {   
        "weight" => typeof(double), // We want weight to always be a decimal number, even if the value in the JSON document has no decimals.
        _ => null // Let JsonDataReader infer the data type for the other properties.
    };            
};

var dataTable = reader.ToDataTable();