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.

rissa
  • 57
  • 1
  • 7
  • 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 – TheGeneral Sep 27 '21 at 05: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. – ProgrammingLlama Sep 27 '21 at 05: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 – rissa Sep 27 '21 at 06:02
  • Why do you need to hardcode it? – ProgrammingLlama Sep 27 '21 at 06:03
  • no I dont want to hardcode – rissa Sep 27 '21 at 06:04
  • Let me rephrase: why do you think that the only way to achieve this is to hardcode it? – ProgrammingLlama Sep 27 '21 at 06:04
  • @Llama like if i get the key as contract 1,then I have to get the particular date from appsettings and if I get contract 3,then the date particular for that contract from appsettings – rissa Sep 27 '21 at 06:06
  • Look, unless you change your JSON to an object of key/value pairs, rather than an array of objects as you currently have, you won't be able to get better performance than by using a loop (see my answer on your similar question [here](https://stackoverflow.com/a/69309165/3181933)). In [this comment](https://stackoverflow.com/questions/69341537/how-to-read-the-value-of-a-particular-key-in-array-of-objects-from-appsettings-f#comment122559501_69341537) you suggested you would have to hardcode values, but I don't understand why you think so. Your reply hasn't cleared that up. – ProgrammingLlama Sep 27 '21 at 06:08
  • @LlamaI saw in one of the comments below,unfortunately it has been deleted by the user now,as var x = configuration.GetSection("AppSettings:contract1");Thats why i told that i dont want to hardcode as i dont know what contract name that i will receive.Depending on the name i get,i have to get the date – rissa Sep 27 '21 at 06:17
  • @rissa That was my suggestion. I don't see why you think string generation has to be hardcoded. What's wrong with `string contractId = "contract1"; string value = configuration[$"AppSettings:contractInfo:{contractId}"]`? Or `string value = configuration.GetSection("AppSettings:contractInfo").Get(contractId);`? You would still need to convert `contractInfo` to an object rather than an array of objects for that to work though. – ProgrammingLlama Sep 27 '21 at 06:21
  • Is changing your JSON to an object absolutely out of the question? – ProgrammingLlama Sep 27 '21 at 06:26
  • Yes @Llama.I want to keep my appsettings json as it is and accomplish the task – rissa Sep 27 '21 at 06:34
  • Then my answer on your other question is the way to do it. Loop through until you find the key you want and return the corresponding value. – ProgrammingLlama Sep 27 '21 at 06:34
  • @LlamaAnd i want to do it without using a for loop as well.because imagine if i get a value like "contract 10" which is not in appsettings,isn't it useless looping?Thats why i asked is there a way that i can read only the value of the key that is required only? – rissa Sep 27 '21 at 06:39
  • I want to drive my car without fuel because fuel is expensive, but unfortunately I live in the real world. I could upgrade to an electric car, but I don't want to. Hopefully this analogy makes it clearer for you? – ProgrammingLlama Sep 27 '21 at 06:47
  • @Llamayou mean answer you provided in https://stackoverflow.com/questions/69308734/how-to-read-values-from-array-of-objects-in-appsettings-json-file/69309071?noredirect=1#comment122510257_69309071 ,cannot be done in any other way without using a for loop? – rissa Sep 27 '21 at 07:52
  • How do you propose getting a specific key from an array where you don't already know its index without using a loop? Think about it logically. – ProgrammingLlama Sep 27 '21 at 07:53
  • Imagine this: you are moving house and you have 4 identical looking cardboard boxes. They're all closed, and the movers have placed them on the floor for you. One contains clothes, the next contains shoes, the next contains your DVDs, and the final box contains cooking supplies. You don't know which order they're in, but you need to cook dinner tonight. How do you find the box? You can't just go and open the cooking supplies box because you don't know which of the 4 that is. You have to visit each box in turn until you find what you're looking for. – ProgrammingLlama Sep 27 '21 at 07:58
  • Now, if the movers had told you "the third box from the left has cooking supplies in it", you could just go straight to that box, but you don't have that information. The only other way would be to randomly open one box at a time in the hope that you get lucky. Is it box 2? Nope. What about box 4? Nope. Box 1? No. OK....it must be box 3. The best approach is to go through each box left-to-right or right-to-left looking for your cooking supplies. – ProgrammingLlama Sep 27 '21 at 07:59
  • Bottom line is: you've said "no" to all of the alternatives (change JSON and/or build a dictionary in C# and then access that by key), so all you can do is loop. – ProgrammingLlama Sep 27 '21 at 08:05
  • @Llama my requirment is,imagine in the db it consist 3 columns(A,B and contract name).in my method there is a call to return the 3 columns(A,B and contract name).After it returns now i know what contract name I have got(contract3).That contract name is a key in the contractInfo object in appsettings.So as I know the exact key,now I want to get the relavant value.(15/10/2021).Assumed all the contract names which are available in db is included in appsettings – rissa Sep 27 '21 at 08:29
  • Anyway thanks a lot @Llama for your time and clarrifications.I am really grateful. – rissa Sep 27 '21 at 08:30
  • No, I do understand your requirement. You're just looking for a magic bullet that doesn't exist. – ProgrammingLlama Sep 27 '21 at 08:32
  • @Llama Thankyou.I changed the appsettings according to the way you suggested – rissa Sep 28 '21 at 03:23

1 Answers1

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"];
Den
  • 650
  • 6
  • 15