-1

I use C# and .net core web api. I have following appsettings.json:

appsettings.json

How can I read the red part (as is, without parsing - raw text) of this appsettings.json?

I know that I can put this in separate file and then read like this:

var googleServiceAccount = File.ReadAllText("GoogleServiceAccount.json");

But I want to put them in appsettings.json. Any ideas?

Serge
  • 28,094
  • 4
  • 11
  • 35
Ka Marius
  • 239
  • 4
  • 19
  • Please [edit] your question to include a detailed description of the problem you have. You can use `File.ReadAllText("appsettings.json");` as well to read the file you want. What seems to be the problem? – Progman Nov 05 '21 at 21:30

3 Answers3

2

You can use Regex.Replace(string input, string pattern, string replacement); method to achieve your requirement:

var sb = new StringBuilder(); sb.AppendLine("{"); 
var googleServiceAccountItems = _configuration.GetSection("Google:ServiceAccount").Get<Dictionary<string, string>>(); foreach (var item in googleServiceAccountItems) { sb.AppendLine($"\t\"{item.Key}\": \"{item.Value}\""); }
sb.Append("}");

var GoogleServiceAccountString = sb.ToString();
GoogleServiceAccountString = Regex.Replace(GoogleServiceAccountString, "\n|\r|\t", String.Empty);
Rena
  • 23,238
  • 5
  • 24
  • 53
1

Your best bet is to change the type of the "ServiceAccount" field to a string, and then take what appears to be uninterpreted JSON and stringify it, escaping appropriately (shown is the \" to escape the embedded quotes).

{
    "Google": {
        "ServiceAccount": "{ \"type\": \"x\", \"project_id\": \"x\" ... }"
    }
}

Also, you can't have multiline strings in JSON,

    "ServiceAccount": "{
       "you": "cant",
       "do": "this"
    }"

so you'd have to insert \\n, which escapes the \ itself leaving a newline \n in the string or try one of the other suggestions in the referenced link.

{
    "Google": {
        "ServiceAccount": "{\\n \"type\": \"x\",\\n \"project_id\": \"x\" ... \\n}"
    }
}

If you really want to somehow designate a portion of the JSON as a string using some sort of magic (e.g. knowing that specific real pieces of JSON should be strings) then you'd have to build your own magic parser (custom configuration provider) that will essentially violate the JSON spec.

I highly recommend you don't do this as it will confuse everyone!

Kit
  • 17,129
  • 4
  • 56
  • 95
1

Try this

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;


IConfiguration configuration = new ConfigurationBuilder()
        .AddJsonFile(@"C:\....\appsettings.json")
        .Build();

 List<KeyValuePair<string,string>> items = configuration
        .GetSection("Google:ServiceAccount")
        .Get<Dictionary<string, string>>()
        .ToList();

foreach (var item in items) Console.WriteLine($"({item.Key},{item.Value})");

or you can use dependency injection instead of configuration builder

public class MyClass
{
   public MyClass(IConfiguration configuration)
{
List<KeyValuePair<string,string>> items = configuration
        .GetSection("Google:ServiceAccount")
        .Get<Dictionary<string, string>>()
        .ToList();
.....
}
}
Serge
  • 28,094
  • 4
  • 11
  • 35
  • Yea, there is possibility to construct that manually like you wrote: var sb = new StringBuilder(); sb.AppendLine("{"); var googleServiceAccountItems = _configuration.GetSection("AppSettings:ExternalServices:Google:ServiceAccount").Get>(); foreach (var item in googleServiceAccountItems) { sb.AppendLine($"\t\"{item.Key}\": \"{item.Value}\""); } sb.Append("}"); GoogleServiceAccountString = sb.ToString(); but I think it is not okey, because values can have like '\n' – Ka Marius Nov 06 '21 at 08:00
  • I mean special characters inside values like '\n' broke everything, and there are some for example in "private_key": "-----BEGIN PRIVATE KEY-----\nMI... – Ka Marius Nov 06 '21 at 08:09
  • @KaMarius What is your problem? You have to try my suggestion and telll if something is not working. YOU didn't mention any special charactesr s in your question but I don' t see how they can affect. Pls post the real code. – Serge Nov 06 '21 at 09:54