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.

5
  • link it might be help you. Commented Feb 7, 2019 at 6:59
  • Just debug the code what you have done. you can able to access that value by index. since you can reassign the value by index. it won't change the index position. Visual Studio's Immediate window may help you more. Commented Feb 7, 2019 at 7:08
  • I added my answer below try it and let me know :) Commented Feb 7, 2019 at 13:40
  • It seems you forgot to view my answer below try it and let me know :) Commented Feb 12, 2019 at 12:40
  • @ArjunBabuC, Glad to hear :) Commented Feb 13, 2019 at 5:24

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

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: dotnetfiddle.net/kqAhMa
@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 :)
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.