using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using Newtonsoft.Json.Linq;
static void IterateJson(JToken value, string mandatoryFieldKey)
if (value.Values().Count() != 0)
foreach (var item in value.Values().ToList())
IterateJson(item, mandatoryFieldKey);
else if (value.ToString() == mandatoryFieldKey)
if (value.Parent.Parent is JObject jObject && jObject["validate"] == null)
Console.WriteLine(value);
jObject.Add("validate", JObject.FromObject(new { required = true }));
public static void Main()
Console.WriteLine("BEFORE MANDATORY");
List<string> mandatoryKeys = new List<string>();
mandatoryKeys.Add("Activity.ActivityDate");
mandatoryKeys.Add("Activity.Duration");
mandatoryKeys.Add("Activity.AgencyActivityCategoryId");
mandatoryKeys.Add("Activity.AgencyActivityAttendanceId");
var data = JObject.Parse(GetRawJson());
for(int i = 0; i < mandatoryKeys.Count(); i++)
IterateJson(data, mandatoryKeys[i]);
Console.WriteLine("AFTER MANDATORY");
Console.WriteLine("XXXXXXXXXXXXXXX");
public static string GetRawJson()
""title"": ""Activity Information"",
""label"": ""Activity Information"",
""key"": ""ActivityInformation"",
""label"": ""Row1Columns"",
""label"": ""Activity Date"",
""format"": ""dd/MM/yyyy hh:mm a"",
""disableWeekends"": false,
""disableWeekdays"": false
""key"": ""Activity.ActivityDate"",
""suffix"": ""<i ref=\""icon\"" class=\""fa fa-calendar\"" style=\""\""></i>"",
""displayInTimezone"": ""viewer"",
""useLocaleSettings"": false,
""format"": ""dd/MM/yyyy hh:mm a"",
""disableWeekends"": false,
""disableWeekdays"": false,
""label"": ""Duration (minutes)"",
""requireDecimal"": false,
""inputFormat"": ""plain"",
""key"": ""Activity.Duration"",
""key"": ""row1Columns"",
""label"": ""Row2Columns"",
""label"": ""Activity Category"",
""widget"": ""choicesjs"",
""custom"": ""values = getActivityCategoryValues()""
""valueProperty"": ""AgencyActivityCategoryId"",
""template"": ""<span>{{ item.text }}</span>"",
""selectThreshold"": 0.3,
""key"": ""Activity.AgencyActivityCategoryId"",
""label"": ""Attendance"",
""widget"": ""choicesjs"",
""custom"": ""values = getAttendanceValues()""
""valueProperty"": ""AgencyActivityAttendanceId"",
""template"": ""<span>{{ item.text }}</span>"",
""selectThreshold"": 0.3,
""key"": ""Activity.AgencyActivityAttendanceId"",
""key"": ""row2Columns"",
""label"": ""Activity Options"",
""widget"": ""choicesjs"",
""custom"": ""values = getActivityManagerValues(data.Activity.AgencyActivityCategoryId)""
""template"": ""<span>{{ item.text }}</span>"",
""refreshOn"": ""Activity.AgencyActivityCategoryId"",
""clearOnRefresh"": true,
""selectThreshold"": 0.3,
""calculateServer"": false,
""key"": ""Activity.ActivityDetail"",
""breadcrumbClickable"": true,