joym8
joym8

Reputation: 4212

How to use Options pattern for appsettings.json complex array

I have the following appsettings.json in a .NET Core Console Application. The examples shown on this Options pattern page do not cover complex type app settings.

How do I iterate through columns for each report type in the following app settings?

{
  "Reports": [
    {
      "name": "Roles",
      "fileName": "roles.csv",
      "columns": [
        {
          "name": "ROLE_ID",
          "default": ""
        },
        {
          "name": "NAME",
          "default": ""
        },
        {
          "name": "AVAILABILITY_IND",
          "default": "YES"
        }
      ]
    },
    {
      "name": "Accounts",
      "fileName": "accounts.csv",
      "columns": [
        {
          "name": "ROLE",
          "default": "NONE"
        },
        {
          "name": "USER_ID",
          "default": ""
        },
        {
          "name": "LASTNAME",
          "default": ""
        },
        {
          "name": "FIRSTNAME",
          "default": ""
        }
      ]
    }
  ]
}

Upvotes: 5

Views: 4403

Answers (1)

Rena
Rena

Reputation: 36645

You could read nested array like below:

public class HomeController : Controller
{
    private readonly IConfiguration _configuration;
    public HomeController(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    public IActionResult Index()
    {
        //read the first report's columns array's first item's name------"ROLE_ID"
        var data = _configuration.GetSection("Reports:0:columns:0:name");
        return View();
    }

For how to foreach all the key and value:

var model = _configuration.GetSection("Reports").AsEnumerable();
foreach (var kv in model)
{
    Console.WriteLine("{0}: {1}", kv.Key, kv.Value);
}

You could also deserialize the json to model and get the item from the model:

Model:

public class Rootobject
{
    public Report[] Reports { get; set; }
}

public class Report
{
    public string name { get; set; }
    public string fileName { get; set; }
    public Column[] columns { get; set; }
}

public class Column
{
    public string name { get; set; }
    public string _default { get; set; }
}

Controller:

var json = System.IO.File.ReadAllText("yourJsonfile.json");
var DeserializeModel = JsonSerializer.Deserialize<Rootobject>(json);

Notes:

If your json file is not default appsettings.json.Someting like:test.json etc.And you also want to read it from IConfiguration,you need remember to register it in Startup.cs:

public class Startup
{
    public Startup(IConfiguration configuration, IWebHostEnvironment env)
    {
        configuration = new ConfigurationBuilder().SetBasePath(env.ContentRootPath)
        .AddJsonFile("test.json")
        .Build();

        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllersWithViews();
        services.AddSingleton<IConfiguration>(Configuration);
    }
}

Upvotes: 3

Related Questions