TL;DR: How do I read a complex JSON object from appsettings.json?
I have a .NET Core 2.x application with multiple types of configuration values. appsettings.json
looks like the snippet below and I am trying to read the value of ElasticSearch:MyIndex:mappings
as a single string or JSON object.
{
"ConnectionStrings": {
"redis": "localhost"
},
"Logging": {
"IncludeScopes": false,
"Debug": {
"LogLevel": {
"Default": "Warning"
}
},
"Console": {
"LogLevel": {
"Default": "Warning"
}
}
},
"ElasticSearch": {
"hosts": [ "http://localhost:9200" ],
"MyIndex": {
"index": "index2",
"type": "mytype",
"mappings": {
"properties": {
"property1": {
"type": "string",
"index": "not_analyzed"
},
"location": {
"type": "geo_point"
},
"code": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}
I am able to read the simple config values (key:value pairs) without issue by calling Configuration.GetValue<string>("ElasticSearch:MyIndex:index")
.
Configuration.GetSection
Configuration.GetSection("ElasticSearch:MyIndex:mappings").Value
gives me a null
value for Value
.
Configuration.GetValue
Configuration.GetValue<string>("ElasticSearch:MyIndex:mappings")
also returns a null value. This makes sense to me as the section has a null value based on the above attempt.
Configuration.GetValue
Configuration.GetValue<JToken>("ElasticSearch:MyIndex:mappings")
also returns a null value. This makes sense to me as well for the same reason as above.
::
in the key... – DavidG Feb 22 '18 at 18:09.Value
on a complex object, that only works forstring
values. – DavidG Feb 22 '18 at 18:11appsettings.json
directly and reading the property I need. But I still would like to see if there are other options. – Babak Naffas Feb 22 '18 at 19:51