Arjun Babu C
Arjun Babu C

Reputation: 35

How to Modify JSON object inside JSON array in c#?

This is my Json Array

[
  {
   "gregdate": "06-03-2019",
   "maldate": "22-07-1194",
   "gregmonth": "March",
   "selected_status": "1"
  },
  {
   "gregdate": "04-05-2019",
   "maldate": "21-09-1194",
   "gregmonth": "May",
   "selected_status": "1"
  },
  {
   "gregdate": "03-06-2019",
   "maldate": "20-10-1194",
   "gregmonth": "June",
   "selected_status": "1"
  }
]

In this JSON Array, I want to change 2nd JSON Object "selected_status" value "1" to "0" without changing the position of the JSON Object.

Upvotes: 0

Views: 6326

Answers (2)

craday
craday

Reputation: 51

This question helped me figure a couple things out - so here is what I came up with. I'm guessing that the json is a sample and what is desired is changing the status for a specific date, rather than just the second element. At least that's what I've been looking for. This is more dynamic and you don't have to worry about the position of the element.

string newJson = "";

if (SwitchStatus(jsonString, "04-05-2019", "0", out newJson))
{
    Console.Write(newJson);
}
else 
{ 
    Console.WriteLine("Date Not Found"); 
}

Console.ReadLine();

static bool SwitchStatus(string jsonString, string searchBy, string switchTo, out string output)
{
    dynamic jsonObj = JsonConvert.DeserializeObject(jsonString);

    JToken status = jsonObj.SelectToken($"$..[?(@.gregdate == '{searchBy}')].selected_status");
    if (status != null)
    {
        status.Replace(switchTo);
        output = JsonConvert.SerializeObject(jsonObj, Newtonsoft.Json.Formatting.Indented);       
    }
    else
    {
        output = jsonString;
    }

    return status != null;

}

enter image description here

Upvotes: 0

er-sho
er-sho

Reputation: 9771

You need to first convert you object array to JArray and then change its second object property from 1 to 0 like

string json = "You json here";                            //Load your json

JArray jArray = JArray.Parse(json);                       //Parse it to JArray

var jObjects = jArray.ToObject<List<JObject>>();          //Get list of objects inside array

foreach (var obj in jObjects)                             //Loop through on a list
{
    if (jObjects.IndexOf(obj) == 1)                       //Get 2nd object from array
    {
        foreach (var prop in obj.Properties())            //List 2nd objects properties
        {
            if (prop.Name == "selected_status")           //Get desired property
                obj["selected_status"] = 0;               //Change its value
        }
    }
}

JArray outputArray = JArray.FromObject(jObjects);         //Output array

Alternative:

As suggested by Brian Rogers you can directly query your JArray to replace its specific property value like,

string json = "You json here";                            //Load your json

JArray jArray = JArray.Parse(json);                       //Parse it to JArray

jArray[1]["selected_status"] = "0";                       //Querying your array to get property of 2nd object

string outputJson = jArray.ToString();                    //Output json

Output: (from debugger)

enter image description here

Upvotes: 4

Related Questions