7

If a configuration file has a nested structure in it, what naming convention or variable name format is used to override that value in a command line environment like Powershell?

.NET config.json file:

{
    "L1a": {
        "L1a2a": {
            "L1a2a1": "value"
        },
        "L2b": false
    },
    "L1b": false
}

From the .NET application code, the configuration values can be accessed like this:

configObject["L1a:L1a2a:L1a2a1"];
configObject["L1a:L2b"];
configObject["L1b"];

Overriding a top level value using an environment variable is easy - just use the same key name:

$env:L1b = "true"

How can I set environment variables in Powershell to override the nested configuration file values?

L1a:L1a2a:L1a2a1 and L1a-L2b are not valid environment variable names. Using underscore like L1a_L2b does not work.

3
  • I think the answer might require using a hash table or dictionary object like @{ key = value }, but I can't get that to work either. Commented Aug 5, 2015 at 18:16
  • It's still not clear to me how this all is related. You have a .NET application that reads a JSON config file. Where exactly do PowerShell and environment variables come into play here? Does the application expose the configuration as environment variables? Commented Aug 5, 2015 at 18:44
  • @AnsgarWiechers it all has to do with the ASP.NET 5 configuration framework. docs.asp.net/en/latest/fundamentals/configuration.html Commented Aug 5, 2015 at 18:49

2 Answers 2

7

You should use __ as seperator for your environment variable hierarchie see aspnetcore-6.0#environment-variables because it is platform agnostic (also applies to .net 5). This is the platform agnostic way of asp .net (will work in docker, linux and linux ...)

${env__L1a__L1a2a__L1a2a1} = 'othervalue'

Sign up to request clarification or add additional context in comments.

1 Comment

Definitely use __ to avoid platform-specific issues
2

You can define/access environment variables with colons (or other special characters) in their names by enclosing the variable name in curly brackets:

${env:L1a:L1a2a:L1a2a1} = 'othervalue'

3 Comments

I'm asking specifically about overriding ASP.NET 5 configuration settings. I either don't understand your answer, or think you've missed the point.
I just tried the curly braces to escape the variable name, and that worked. Thanks! The rest of the answer isn't really relevant. Sorry for the confusion around ASP.NET 5.
How do you save nested config data into an environment variable? I have '__' but didn't worked

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.