I'm starting with this JSON:
{
"name": "company",
"type": "record",
"fields": [{
"name": "WorkStatus",
"type": "string"
},
{
"name": "DeploymentCode",
"type": "string"
},
{
"name": "entity",
"type": "string"
},
{
"name": "lastUpdatedDate",
"type": "string"
},
{
"name": "lastUpdatedBy",
"type": "string"
},
{
"name": "EffectiveDate",
"type": "string"
}]
}
I want to add (default) to each of the fields so I end up with this:
{
"name": "company",
"type": "record",
"fields": [{
"name": "WorkStatus",
"type": "string",
"default": "null"
},
{
"name": "DeploymentCode",
"type": "string",
"default": "null"
},
{
"name": "entity",
"type": "string",
"default": "null"
},
{
"name": "lastUpdatedDate",
"type": "string",
"default": "null"
},
{
"name": "lastUpdatedBy",
"type": "string",
"default": "null"
},
{
"name": "EffectiveDate",
"type": "string",
"default": "null"
}]
}
I have this code so far but I blow up on the Add:
JObject entireSchema = JObject.Parse(jsonResult.ToString());
JArray fieldsArray = (JArray)entireSchema["fields"];
foreach (var field in fieldsArray)
{
field.AddAfterSelf(new JProperty("default", "null"));
}
I know this be something small but I don't have a complete grasp of all the JObject and JArray functions. What am I missing to get this added to each of the fields?
string jsonString = "{'name':'company','type':'record','fields':[{'name':'WorkStatus','type':'string'},{'name':'DeploymentCode','type':'string'},{'name':'entity','type':'string'},{'name':'lastUpdatedDate','type':'string'},{'name':'lastUpdatedBy','type':'string'},{'name':'EffectiveDate','type':'string'}]}";
//Note: You must convert to JObject
var jsonObject = JObject.Parse(jsonString);
var jsonFields = jsonObject["fields"];
List<JObject> jsonList = new List<JObject>();
foreach (var item in jsonFields)
{
var tempJson = JObject.Parse(item.ToString());
tempJson.Add("default", "null");
jsonList.Add(tempJson);
}
var getListJson = JToken.FromObject(jsonList);
//Fixed! :)
jsonObject.Remove("fields");
jsonObject.Add("fields", getListJson);
that's it!