Reading a JSON object from appsettings.json

The solution ended up being much simpler than anything I was initially trying: read appsettings.json as any other JSON formatted file.

JToken jAppSettings = JToken.Parse(
  File.ReadAllText(Path.Combine(Environment.CurrentDirectory, "appsettings.json"))
);

string mapping = jAppSettings["ElasticSearch"]["MyIndex"]["mappings"];

Convert your JSON object to an escaped string. To do this you'll most likely just have to escape all the double quotes and put it on one line so it looks something like:

"ElasticSearch": "{\"hosts\": [ \"http://localhost:9200\" ],\"MyIndex\": {\"index\"... "

Then you could read it into a string that can be parsed by just using:

Configuration["ElasticSearch"]

This solution isn't for everyone since it's not fun to look at or update the escaped json, but if you only plan on rarely making changes to this configuration setting then it might not be the worst idea.


Dictionary<string,object> settings = Configuration
    .GetSection("ElasticSearch")
    .Get<Dictionary<string,object>>();
string json = JsonConvert.SerializeObject(settings);