1

My appsettings json file

    {
          "AppSettings": {
             "contractInfo": [
                    { "contract1": "01/11/2020"},
                    { "contract2": "29/07/2021"},
                    { "contract3": "15/10/2021"},
                    { "contract4": "15/02/2021"}
                ]
            }
}

I have a seperate config class.

public ConfigHelper(IConfiguration configuration)
{
    _configuration = configuration;
}
public string GetConfigValue(string key)
{
    var value = _configuration["AppSettings:" + key];
    return !string.IsNullOrEmpty(value) ? Convert.ToString(value) : string.Empty;
}

Contract Info is as key value pair,where the key is the contarct name and the value is the contract end date.

How can I read the end date of the particular contract without using a for loop from the appsettings.json file.(That is, if we get the contract name as "contract 3" from the database,I want to read the contract end date of contract 3 from the appsettingsjson without using a for loop)

Any help is appreciated. Thankyou.

24
  • This json is a little strange, you have an array of objects all with different property names, yes you can likely do this with some funky converter or binder, or provider, which would all be more hassle than its worth. It seems you should just change your json to an actual dictionary if at all possible Commented Sep 27, 2021 at 5:44
  • I think it's worth noting that, unless you know the exact index in the array that you're intetested in, you can't really access a specific value in an array without some kind of looping mechanism, either directly or within .NET Core itself. If you change contractInfo to an object instead of an array, the keys can be accessed in a more performant way. Commented Sep 27, 2021 at 5:50
  • @Llama I want to keep my appsettings in this format itself and complete the task(as it is just a one part of already implemented project).is there a way that i can read the value of the particular key only without hardcoding as contract1 because depending on the name we get from the db.(it may differ from one user to another) ,i have to get the date Commented Sep 27, 2021 at 6:02
  • Why do you need to hardcode it? Commented Sep 27, 2021 at 6:03
  • no I dont want to hardcode Commented Sep 27, 2021 at 6:04

1 Answer 1

1

You can load this values into a dictionary (see that link)

Then you can load any option with the key name.

EDIT

You must change your appconfig.json into this:

"contractInfo": {
     "contract1": "01/11/2020",
     "contract2": "29/07/2021",
     "contract3": "15/10/2021",
     "contract4": "15/02/2021"
  }

then you create a custom class:

public class ApplicationSettings
{
    public Dictionary<string, string> contractInfo { get; set; }
}

And in sturtup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<ApplicationSettings>(Configuration);
}

Then you can use this value like a Dictionary:

var appSettings = services.BuildServiceProvider().GetRequiredService<IOptions<ApplicationSettings>>().Value;

var dValue = appSettings.contractInfo["contract1"];
Sign up to request clarification or add additional context in comments.

8 Comments

Could you provide some sample code to go with this answer, as the question you've linked to doesn't seem to answer it.
It's difficult without a pc create an example... :) Next time I'm doing it...
I'm hoping it's not something like this?
Thegeneral answare this question with the same method and he add some pice of example...
OK that definitely makes sense now with the advice to change the JSON. :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.