10

I'm trying to add a property to a json object, which are not root of the json.

example is below.

{
    'isFile' : 'true',
    'Values' : {
        'valueName1': 'value1',
        'valueName2': 'value2',
        'valueName3': 'value3',
    }
}

after the operation, i want the json file to look like below.

{
    'isFile' : 'true',
    'Values' : {
        'valueName1': 'value1',
        'valueName2': 'value2',
        'valueName3': 'value3',
        'valueName4': 'value4'
    }
}

I have gotten to the point where I can access Values property through below code. Where do I go next?

JObject appSettings = JsonConvert.DeserializeObject<JObject>(jsonString);
string values = appSettings["Values"].ToString();

any help?

*Edit I'm trying to edit values section for local.settings.json file for azure app function in Visual Studio.

8
  • Are you trying to access AppSettings like this? You know there are already built in ways of doing this... Commented Apr 23, 2018 at 20:11
  • 3
    JObject appSettings = JsonConvert.DeserializeObject<JObject>(jsonString); appSettings["Values"]["valueName4"] = "value4"; Commented Apr 23, 2018 at 20:14
  • learn how to code json props that are arrays. you are hard coding a list. Commented Apr 23, 2018 at 20:26
  • Yeah I'm trying to access app settings for azure function. how do I do it? Commented Apr 23, 2018 at 20:31
  • how do I do it? - @CodeNotFound already gave you the answer: use the indexer. See How do you Add or Update a JProperty Value in a JObject. Commented Apr 23, 2018 at 20:40

1 Answer 1

17

you can do it with a dynamic object

        dynamic obj = JsonConvert.DeserializeObject<ExpandoObject>(jsonString);
        obj.Values.valueName4 = "value4";
        System.Console.WriteLine(JsonConvert.SerializeObject(obj));
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.