JsonConfigurationProvider

The JsonConfigurationProvider class allows you to load configuration data from a JSON file, JSON string, or JSON stream. Consider the following JSON:

{
    "Logging": {
        "Default": "Debug"
    },
    "ConnectionStrings": {
        "SampleDb": "my_cxn_string"
    },
}

When loaded using JsonConfigurationProvider the resulting Configuration object will have attribute names and a structure which matches the JSON, consider the following (where “example.json” contains the above JSON):

from appsettings2 import *
from appsettings2.providers import *

config = ConfigurationBuilder()\
    .addProvider(JsonConfigurationProvider('example.json'))\
    .build()

print(config['LOGGING_DEFAULT']) # outputs: "Debug"
print(config.ConnectionStrings.SampleDb) # outputs: "my_cxn_string"

class JsonConfigurationProvider(filepath: str | None = None, *, json: str | None = None, fd: int | None = None, required: bool = True)

Bases: ConfigurationProvider

Populates structured configuration data from JSON.

The filepath, json, and fd parameters are mutually exclusive.

Parameters:
  • filepath – Optional path to a JSON file used as a configuration source, defaults to None.

  • json – Optional JSON string used as a configuration source, defaults to None.

  • fd – Optional file descriptor (int) to be used as a configuration source, defaults to None.

  • required – Optional parameter indicating whether the configuration source will raise ConfigurationException if the specified configuration source is missing, defaults to True.

populateConfiguration(configuration: Configuration)

Populates the provided Configuration object using provider-specific methods.