Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a json string as a app setting value in config file

I have a requirement whereby I need to fetch a set of jsons before making a API call. I am planning to add these json strings in app.config as shown below

 <add key="Jsons" value="{""Id"":""25"",""Name"":""Value-1""}"/>

However adding this results in a compilation error "Missing whitespace" at the start of the value. Please let me know if i am missing something. I dont want to create a separate text file to read jsons from. Thats why i decided to use app.config itself

like image 381
Shaiju Janardhanan Avatar asked Feb 11 '16 04:02

Shaiju Janardhanan


People also ask

Can JSON be used for config file?

JSON format, intended initially to replace XML for data-interchange, is widely used today to provide public data using APIs and web services as well as to write various configuration files.

What is app config JSON?

config. js, app. config. ts) is used for configuring how a project loads in Expo Go, Expo Prebuild generation, and the OTA update manifest.

How can I add a custom JSON file into IConfiguration?

CreateDefaultBuilder(args) //This is how you attach additional JSON files . ConfigureAppConfiguration((hostingContext, config) => { config. AddJsonFile("customSettings. json", optional: false, reloadOnChange: false); }) //End of update .


2 Answers

Your quotes are not correctly formatted. Can you try this:

<add key="Jsons" value='{"Id":"25","Name":"Value-1"}'/>

like image 55
Louie Almeda Avatar answered Oct 28 '22 22:10

Louie Almeda


An app.config is still XML! You need to use the XML escape sequence for quotes.

<add key="Jsons" value="{&quot;Id&quot;:&quot;25&quot;,&quot;Name&quot;:&quot;Value-1&quot;}"/>
like image 2
Kevin Burdett Avatar answered Oct 28 '22 22:10

Kevin Burdett