0

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.

Gowtham Alan
  • 234
  • 3
  • 15
Arjun Babu C
  • 35
  • 1
  • 8

2 Answers2

4

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

er-sho
  • 9,581
  • 2
  • 13
  • 26
  • FYI, most of this code is unnecessary. You can index into a JArray directly; you do not need to convert it to a list of JObjects first. Also, you don't need to loop over all properties in order to set a specific one. After parsing the JSON to a JArray you can just do `jArray[1]["selected_status"] = "0";` and then convert it back to JSON string using `ToString()`. Fiddle: https://dotnetfiddle.net/kqAhMa – Brian Rogers Feb 07 '19 at 17:18
  • @BrianRogers, Thanks for your suggestion, but first time I did it like yours but later I thought OP want more simplify code and working of how to replace any key value so thats why I rewrite to this way, but now I think I have to provide that code also, thanks one more time :) – er-sho Feb 07 '19 at 18:10
0

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

craday
  • 21
  • 3