I have a json file with the following format:
{
"value": [
{
"ccCode": "1234",
"attributes": {
"ePCode": 23467
}
},
{
"ccCode": "1234",
"attributes": {
"ePCode": 23467
}
}
]
}
I simplified the json but it has a whole lot more of fields and more nested properties within attributes property. I'm trying to iterate through the objects in the value array, and retrieve two of its properties. Based on those two values I will fetch a 3rd value and want to add it to the attributes property as an additional prop. So in the end I want:
{
"value": [
{
"ccCode": "1234",
"attributes": {
"ePCode": 23467,
"newFlag": true
}
},
{
"ccCode": "1234",
"attributes": {
"ePCode": 23467,
"newFlag": false
}
}
]
}
I created the code below. So far I can fetch my properties of interest. My problem is trying to update it. Any help will be greatly appreciated.
string templateFile = @"C:\Users\template.json";
//rss
JObject template = JObject.Parse(File.ReadAllText(templateFile));
foreach (var record in template)
{
string name = record.Key;
JToken value = record.Value;
foreach(var obj in value)
{
//fetch two values from each json object,
//based on these , fetch a flag and then add it to the json object
var ep = obj["attributes"];
ep = ep["ePCode"].Value<int?>();
var cost = obj["ccCode"].ToString();
bool isCostValuable = isCostValuable((int)ep, cost);
///want to add a new property in the property attributes
//this is where I get stuck
foreach(JProperty prop in obj)
{
if (prop.Name == "attributes")
{
JObject first = (JObject)obj;
JArray item = (JArray)first["attributes"];
}
}
}
}
obj["attributes"].Add("newFlag", isCostValuable);