0

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
  • 40,935
  • 4
  • 18
  • 45
  • 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
  • @Progman, "part of a file" is not the same as "file". – greenoldman Oct 04 '22 at 20:53

2 Answers2

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
  • 30,832
  • 6
  • 37
  • 72
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
  • 40,935
  • 4
  • 18
  • 45
  • 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' – Marius Kazlauskas 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... – Marius Kazlauskas 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