Have A JSon Array Like This inside my JSON File
{
"userId": "UserIDA",
"SNNO": "1234_1212",
"dateTime": "2023-06-30T06:39:28.3340878+08:00",
"
"Results": [
{
"Order": "1",
"OrderStatus": "Received",
"OrderDetails": [
{
"OName": "Packing",
"OStatus": "22",
"OValue": "24"
},
{
"OName": "INvoice",
"OStatus": "24",
"OValue": "29"
},
{
"OName": "LIst",
"OStatus": "56",
"OValue": "44"
}
]
}
]
}
Through Dynamically i need to modify the values for all the Orderdetails(0,1,2).Plz help

Amit MohantyRank #112 · 20K reputationPosted Jun 30, 2023, 1:26 AM EDT
using System; using System.IO; using Newtonsoft.Json; using Newtonsoft.Json.Linq; class Program { static void Main() { // Read the JSON file into a string string jsonData = File.ReadAllText("path/to/your/file.json"); try { // Parse the JSON data JObject data = JObject.Parse(jsonData); // Access the OrderDetails array and loop through each item JArray orderDetails = (JArray)data["Results"][0]["OrderDetails"]; foreach (JObject orderDetail in orderDetails) { // Modify the desired properties within each OrderDetails orderDetail["OStatus"] = "NewStatus"; orderDetail["OValue"] = "NewValue"; } // Convert the updated JSON data back to a string string updatedJsonData = data.ToString(); // Now you can save the updatedJsonData to your file or use it as needed // For example, if you want to save it back to the file, you can do: File.WriteAllText("path/to/your/file.json", updatedJsonData); } catch (Exception ex) { Console.WriteLine("Error processing JSON data: " + ex.Message); } } }Brahma Prakash ShuklaRank #633 · 3.8K reputationPosted Jun 29, 2023, 11:23 PM EDT
To modify the values for all the
OrderDetailsin the given JSON, you can use JavaScript or any other programming language that supports JSON manipulation. Here's an example using JavaScript:// Assuming the JSON data is stored in a variable called jsonData
var jsonData = {
"userId": "UserIDA",
"SNNO": "1234_1212",
"dateTime": "2023-06-30T06:39:28.3340878+08:00",
"Results": [
{
"Order": "1",
"OrderStatus": "Received",
"OrderDetails": [
{
"OName": "Packing",
"OStatus": "22",
"OValue": "24"
},
{
"OName": "INvoice",
"OStatus": "24",
"OValue": "29"
},
{
"OName": "LIst",
"OStatus": "56",
"OValue": "44"
}
]
}
]
};
// Modifying the values for all OrderDetails
jsonData.Results[0].OrderDetails.forEach(function(orderDetail) {
orderDetail.OStatus = "NewStatus";
orderDetail.OValue = "NewValue";
});
// Printing the modified JSON
console.log(JSON.stringify(jsonData, null, 2));