I have below Dynamic nested JSON in which a node can contain multiple child nodes. Each node can have any number of sibilings. Also a Node can have child up to any number of level of depth.
Now i have task of adding a new node either as sibiling before, after or as child to a particular node. based on matching text. for example I pass a SearchText= "How are you doing?". so logic should work like this.
- first it will find node Say 'MatchedNode' which has text matching with SearchText.
- If Selected Option is 'Before' It will create a new blank node and add it to before 'MatchedNode'
- If Selected Option is 'After' It will create a new blank node and add it to After 'MatchedNode'
- If Selected Option is 'Child' It will create a new blank node and add it as child node to 'MatchedNode'
I am using recursion to iterate through nested JSON.
private void NewSaveDraft()
{
InMemoryFileData = File.ReadAllText(filelocation);
dynamic data = JObject.Parse(InMemoryFileData);
for (int sectionIndex = 0; sectionIndex < data.Sections.Count; sectionIndex++)
{
if (data.Sections[sectionIndex].Name == JSONTreeView.SelectedNode.Tag)
{
SaveDraftData(data.Sections[sectionIndex]);
return;
}
else
{
foreach (var node in data.Sections[sectionIndex].Nodes)
findSelectedNode(node);
}
}
}
private void findSelectedNode(dynamic node)
{
if (node.Tag == JSONTreeView.SelectedNode.Tag)
{
SaveDraftData(node);
return;
}
else
foreach (var n in node.Nodes)
findSelectedNode(n);
}
private void SaveDraftData(dynamic node)
{
ChangeNodeDataForSelectedNode(node);
}
private void ChangeNodeDataForSelectedNode(dynamic obj)
{
if (SelectedContextMenuOption.Equals("C"))
obj.Nodes.Add(new Node());
}
here definition of a node is below
{
"Actions": [
{
"Rule": "",
"ActionCmds": [
{
"ActionCmd": "",
"ParentNode": ""
}
]
}
],
"Name": "ChildDepthLevel4",
"Text": "ChildDepthLevel4",
"HelpText": "",
"InputCaptureRules": {
"Min": 0,
"Max": 0
},
"Tag": "ChildDepthLevel4",
"IsRoot": true,
"IsOptional": false,
"IsLabel": false,
"Note": {
"Format": "",
"Items": [
{
"Style": "",
"Text": ""
}
]
},
"HasPlaceHolderInText": false,
"Benefits": [
""
],
"Style": "",
"IsConfirmationRequired": false,
"Nodes": []
}
a New Blank Node is like below
{
"Actions": [
{
"Rule": "",
"ActionCmds": [
{
"ActionCmd": "",
"ParentNode": ""
}
]
}
],
"Name": "",
"Text": "",
"HelpText": "",
"InputCaptureRules": {
"Min": 0,
"Max": 0
},
"Tag": "",
"IsRoot": true,
"IsOptional": false,
"IsLabel": false,
"Note": {
"Format": "",
"Items": [
{
"Style": "",
"Text": ""
}
]
},
"HasPlaceHolderInText": false,
"Benefits": [
""
],
"Style": "",
"IsConfirmationRequired": false,
"Nodes": []
}
I am using recursion to iterate through nested JSON.
Below is sample JSON
{
"Name": "General H&L",
"Version": "1.2",
"NameSpace": "",
"Sections": [
{
"Name": "Height-weight",
"Nodes": [
{
"Actions": [
{
"Rule": "(Whatisyourexactheightincmsfeetandinches4604 = \"Cms\")",
"ActionCmds": [
{
"ActionCmd": "OpenNode 4605",
"ParentNode": null
}
]
},
{
"Rule": "(Whatisyourexactheightincmsfeetandinches4604 = \"Feet & Inches\")",
"ActionCmds": [
{
"ActionCmd": "OpenNode 4606",
"ParentNode": null
},
{
"ActionCmd": "OpenNode 4607",
"ParentNode": null
}
]
}
],
"Name": "4604",
"Text": "What is your exact height in cms, feet and inches?",
"HelpText": null,
"InputCaptureRules": {},
"Tag": "Whatisyourexactheightincmsfeetandinches4604",
"IsRoot": true,
"IsOptional": false,
"IsLabel": false,
"Note": null,
"HasPlaceHolderInText": false,
"Benefits": [
"DTH",
"TPD",
"TRA"
],
"Style": null,
"IsConfirmationRequired": false,
"Nodes": [
{
"Actions": [
{
"Rule": "(HeightCms4605 >= 90) AND (HeightCms4605 <= 220)",
"ActionCmds": [
{
"ActionCmd": "Calculator, BMI, BMI",
"ParentNode": null
}
]
}
],
"Name": "4605",
"Text": "Height (Cms)",
"HelpText": null,
"InputCaptureRules": {
"Min": 90,
"Max": 220
},
"Tag": "HeightCms4605",
"IsRoot": false,
"IsOptional": false,
"IsLabel": false,
"Note": null,
"HasPlaceHolderInText": false,
"Benefits": [
"DTH",
"TPD",
"TRA"
],
"Style": null,
"IsConfirmationRequired": false,
"Nodes": []
},
{
"Actions": [
{
"Rule": "(HeightFeet4606 >= 3) AND (HeightFeet4606 <= 7)",
"ActionCmds": [
{
"ActionCmd": "Calculator, BMI, BMI",
"ParentNode": null
}
]
}
],
"Name": "4606",
"Text": "Height (Feet)",
"HelpText": null,
"InputCaptureRules": {
"Min": 3,
"Max": 7
},
"Tag": "HeightFeet4606",
"IsRoot": false,
"IsOptional": false,
"IsLabel": false,
"Note": null,
"HasPlaceHolderInText": false,
"Benefits": [
"DTH",
"TPD",
"TRA"
],
"Style": null,
"IsConfirmationRequired": false,
"Nodes": []
},
{
"Actions": [
{
"Rule": "(HeightInches4607 >= 0) AND (HeightInches4607 <= 12)",
"ActionCmds": [
{
"ActionCmd": "Calculator, BMI, BMI",
"ParentNode": null
}
]
}
],
"Name": "4607",
"Text": "Height (Inches)",
"HelpText": null,
"InputCaptureRules": {
"Min": 0,
"Max": 12
},
"Tag": "HeightInches4607",
"IsRoot": false,
"IsOptional": false,
"IsLabel": false,
"Note": null,
"HasPlaceHolderInText": false,
"Benefits": [
"DTH",
"TPD",
"TRA"
],
"Style": null,
"IsConfirmationRequired": false,
"Nodes": []
}
]
},
{
"Actions": [
{
"Rule": "(Whatisyourexactweightinkgslbsorstones4608 = \"Kgs\")",
"ActionCmds": [
{
"ActionCmd": "OpenNode 4609",
"ParentNode": null
}
]
},
{
"Rule": "(Whatisyourexactweightinkgslbsorstones4608 = \"Lbs\")",
"ActionCmds": [
{
"ActionCmd": "OpenNode 4610",
"ParentNode": null
}
]
},
{
"Rule": "(Whatisyourexactweightinkgslbsorstones4608 = \"Stones\")",
"ActionCmds": [
{
"ActionCmd": "OpenNode 4611",
"ParentNode": null
},
{
"ActionCmd": "OpenNode 4612",
"ParentNode": null
}
]
}
],
"Name": "4608",
"Text": "What is your exact weight in kgs, lbs or stones?",
"HelpText": null,
"InputCaptureRules": {},
"Tag": "Whatisyourexactweightinkgslbsorstones4608",
"IsRoot": true,
"IsOptional": false,
"IsLabel": false,
"Note": null,
"HasPlaceHolderInText": false,
"Benefits": [
"DTH",
"TPD",
"TRA"
],
"Style": null,
"IsConfirmationRequired": false,
"Nodes": [
{
"Actions": [
{
"Rule": "(WeightKgs4609 > 0)",
"ActionCmds": [
{
"ActionCmd": "Calculator, BMI, BMI",
"ParentNode": null
}
]
}
],
"Name": "4609",
"Text": "Weight (Kgs)",
"HelpText": null,
"InputCaptureRules": {
"Min": 1
},
"Tag": "WeightKgs4609",
"IsRoot": false,
"IsOptional": false,
"IsLabel": false,
"Note": null,
"HasPlaceHolderInText": false,
"Benefits": [
"DTH",
"TPD",
"TRA"
],
"Style": null,
"IsConfirmationRequired": false,
"Nodes": []
},
{
"Actions": [
{
"Rule": "(WeightLbs4610 > 0)",
"ActionCmds": [
{
"ActionCmd": "Calculator, BMI, BMI",
"ParentNode": null
}
]
}
],
"Name": "4610",
"Text": "Weight (Lbs)",
"HelpText": null,
"InputCaptureRules": {
"Min": 1
},
"Tag": "WeightLbs4610",
"IsRoot": false,
"IsOptional": false,
"IsLabel": false,
"Note": null,
"HasPlaceHolderInText": false,
"Benefits": [
"DTH",
"TPD",
"TRA"
],
"Style": null,
"IsConfirmationRequired": false,
"Nodes": []
},
{
"Actions": [
{
"Rule": "(WeightStones4611 > 0)",
"ActionCmds": [
{
"ActionCmd": "Calculator, BMI, BMI",
"ParentNode": null
}
]
}
],
"Name": "4611",
"Text": "Weight (Stones)",
"HelpText": null,
"InputCaptureRules": {
"Min": 1
},
"Tag": "WeightStones4611",
"IsRoot": false,
"IsOptional": false,
"IsLabel": false,
"Note": null,
"HasPlaceHolderInText": false,
"Benefits": [
"DTH",
"TPD",
"TRA"
],
"Style": null,
"IsConfirmationRequired": false,
"Nodes": []
},
{
"Actions": [
{
"Rule": "(WeightLbs4612 >= 0) AND (WeightLbs4612 < 14)",
"ActionCmds": [
{
"ActionCmd": "Calculator, BMI, BMI",
"ParentNode": null
}
]
}
],
"Name": "4612",
"Text": "Weight (Lbs)",
"HelpText": null,
"InputCaptureRules": {
"Min": 0,
"Max": 13
},
"Tag": "WeightLbs4612",
"IsRoot": false,
"IsOptional": false,
"IsLabel": false,
"Note": null,
"HasPlaceHolderInText": false,
"Benefits": [
"DTH",
"TPD",
"TRA"
],
"Style": null,
"IsConfirmationRequired": false,
"Nodes": []
}
]
},
{
"Actions": [
{
"Rule": "(Areyounoworhaveyoueverreceivedtreatmentforanorexiaorbulimia4614 = \"No\")",
"ActionCmds": [
{
"ActionCmd": "OpenNode 4615",
"ParentNode": null
}
]
},
{
"Rule": "(Areyounoworhaveyoueverreceivedtreatmentforanorexiaorbulimia4614 = \"Yes\")",
"ActionCmds": [
{
"ActionCmd": "Outcome, Decline, DTH, 'due to BMI >= 16.0 and < 17 and having received treatment for anorexia or bulimia', 1069",
"ParentNode": null
},
{
"ActionCmd": "Outcome, Decline, TRA, 'due to BMI >= 16.0 and < 17 and having received treatment for anorexia or bulimia', 1069",
"ParentNode": null
}
]
}
],
"Name": "4614",
"Text": "Are you now or have you ever received treatment for anorexia or bulimia?",
"HelpText": null,
"InputCaptureRules": {},
"Tag": "Areyounoworhaveyoueverreceivedtreatmentforanorexiaorbulimia4614",
"IsRoot": false,
"IsOptional": false,
"IsLabel": false,
"Note": null,
"HasPlaceHolderInText": false,
"Benefits": [
"DTH",
"TRA"
],
"Style": null,
"IsConfirmationRequired": false,
"Nodes": []
},
{
"Actions": [
{
"Rule": "(Haveyouexperiencedanyunexplainedweightlossofmorethanthreekgorsevenlbsinthelasttwelvemonths4615 = \"Yes\")",
"ActionCmds": [
{
"ActionCmd": "Outcome, Decline, DTH, 'due to BMI >= 16.0 and < 17 and having unexplained weight loss', 1068",
"ParentNode": null
},
{
"ActionCmd": "Outcome, Decline, TRA, 'due to BMI >= 16.0 and < 17 and having unexplained weight loss', 1068",
"ParentNode": null
}
]
},
{
"Rule": "(Haveyouexperiencedanyunexplainedweightlossofmorethanthreekgorsevenlbsinthelasttwelvemonths4615 = \"No\") AND (Smoker = \"Non Smoker\")",
"ActionCmds": [
{
"ActionCmd": "LoadingPercent, DTH, 'LOADING: 50% due to BMI <17', 50.0000%, '', 565",
"ParentNode": null
},
{
"ActionCmd": "LoadingPercent, TRA, 'LOADING: 50% due to BMI <17', 50.0000%, '', 565",
"ParentNode": null
}
]
},
{
"Rule": "(Haveyouexperiencedanyunexplainedweightlossofmorethanthreekgorsevenlbsinthelasttwelvemonths4615 = \"No\") AND (Smoker = \"Smoker\")",
"ActionCmds": [
{
"ActionCmd": "LoadingPercent, DTH, 'LOADING: 75% due to BMI <17 and being a smoker', 75.0000%, '', 566",
"ParentNode": null
},
{
"ActionCmd": "LoadingPercent, TRA, 'LOADING: 75% due to BMI <17 and being a smoker', 75.0000%, '', 566",
"ParentNode": null
}
]
}
],
"Name": "4615",
"Text": "Have you experienced any unexplained weight loss of more than 3kg or 7 lbs in the last 12 months?",
"HelpText": null,
"InputCaptureRules": {},
"Tag": "Haveyouexperiencedanyunexplainedweightlossofmorethanthreekgorsevenlbsinthelasttwelvemonths4615",
"IsRoot": false,
"IsOptional": false,
"IsLabel": false,
"Note": null,
"HasPlaceHolderInText": false,
"Benefits": [
"DTH",
"TRA"
],
"Style": null,
"IsConfirmationRequired": false,
"Nodes": []
},
{
"Actions": [
{
"Rule": "(Areyounoworhaveyoueverreceivedtreatmentforanorexiaorbulimia4617 = \"No\")",
"ActionCmds": [
{
"ActionCmd": "OpenNode 4618",
"ParentNode": null
}
]
},
{
"Rule": "(Areyounoworhaveyoueverreceivedtreatmentforanorexiaorbulimia4617 = \"Yes\")",
"ActionCmds": [
{
"ActionCmd": "Outcome, Decline, DTH, 'due to BMI >= 17 & BMI < 18 and have received treatment for anorexia', 1070",
"ParentNode": null
},
{
"ActionCmd": "Outcome, Decline, TRA, 'due to BMI >= 17 & BMI < 18 and have received treatment for anorexia', 1070",
"ParentNode": null
}
]
}
],
"Name": "4617",
"Text": "Are you now or have you ever received treatment for anorexia or bulimia?",
"HelpText": null,
"InputCaptureRules": {},
"Tag": "Areyounoworhaveyoueverreceivedtreatmentforanorexiaorbulimia4617",
"IsRoot": false,
"IsOptional": false,
"IsLabel": false,
"Note": null,
"HasPlaceHolderInText": false,
"Benefits": [
"DTH",
"TPD",
"TRA"
],
"Style": null,
"IsConfirmationRequired": false,
"Nodes": [
{
"Actions": [
{
"Rule": "(Haveyouexperiencedanyunexplainedweightlossofmorethanthreekgorsevenlbsinthelasttwelvemonths4618 = \"Yes\")",
"ActionCmds": [
{
"ActionCmd": "OpenNode 4619",
"ParentNode": null
},
{
"ActionCmd": "OpenNode 4620",
"ParentNode": null
},
{
"ActionCmd": "OpenNode 4621",
"ParentNode": null
},
{
"ActionCmd": "OpenNode 4622",
"ParentNode": null
},
{
"ActionCmd": "Outcome, Refer, DTH, 'Build', 400",
"ParentNode": null
},
{
"ActionCmd": "Outcome, Refer, TPD, 'Build', 400",
"ParentNode": null
},
{
"ActionCmd": "Outcome, Refer, TRA, 'Build', 400",
"ParentNode": null
}
]
},
{
"Rule": "(Haveyouexperiencedanyunexplainedweightlossofmorethanthreekgorsevenlbsinthelasttwelvemonths4618 = \"No\")",
"ActionCmds": [
{
"ActionCmd": "LoadingPercent, DTH, 'LOADING: 50% due to BMI 17 or <18', 50.0000%, '', 567",
"ParentNode": null
}
]
}
],
"Name": "4618",
"Text": "Have you experienced any unexplained weight loss of more than 3kg or 7 lbs in the last 12 months?",
"HelpText": null,
"InputCaptureRules": {},
"Tag": "Haveyouexperiencedanyunexplainedweightlossofmorethanthreekgorsevenlbsinthelasttwelvemonths4618",
"IsRoot": false,
"IsOptional": false,
"IsLabel": false,
"Note": null,
"HasPlaceHolderInText": false,
"Benefits": [
"DTH",
"TPD",
"TRA"
],
"Style": null,
"IsConfirmationRequired": false,
"Nodes": [
{
"Actions": [],
"Name": "4619",
"Text": "How much weight have you lost?",
"HelpText": null,
"InputCaptureRules": {},
"Tag": "Howmuchweighthaveyoulost4619",
"IsRoot": false,
"IsOptional": false,
"IsLabel": false,
"Note": null,
"HasPlaceHolderInText": false,
"Benefits": [
"DTH",
"TPD",
"TRA"
],
"Style": null,
"IsConfirmationRequired": false,
"Nodes": []
},
{
"Actions": [],
"Name": "4620",
"Text": "Did you consult a health professional regarding your weight loss?",
"HelpText": null,
"InputCaptureRules": {},
"Tag": "Didyouconsultahealthprofessionalregardingyourweightloss4620",
"IsRoot": false,
"IsOptional": false,
"IsLabel": false,
"Note": null,
"HasPlaceHolderInText": false,
"Benefits": [
"DTH",
"TPD",
"TRA"
],
"Style": null,
"IsConfirmationRequired": false,
"Nodes": []
},
{
"Actions": [],
"Name": "4621",
"Text": "What investigations or treatment was recommended/performed?",
"HelpText": null,
"InputCaptureRules": {},
"Tag": "Whatinvestigationsortreatmentwasrecommendedperformed4621",
"IsRoot": false,
"IsOptional": false,
"IsLabel": false,
"Note": null,
"HasPlaceHolderInText": false,
"Benefits": [
"DTH",
"TPD",
"TRA"
],
"Style": null,
"IsConfirmationRequired": false,
"Nodes": []
},
{
"Actions": [],
"Name": "4622",
"Text": "If you have had these investigations what were the results?",
"HelpText": null,
"InputCaptureRules": {},
"Tag": "Ifyouhavehadtheseinvestigationswhatweretheresults4622",
"IsRoot": false,
"IsOptional": false,
"IsLabel": false,
"Note": null,
"HasPlaceHolderInText": false,
"Benefits": [
"DTH",
"TPD",
"TRA"
],
"Style": null,
"IsConfirmationRequired": false,
"Nodes": []
}
]
}
]
},
{
"Actions": [
{
"Rule": "(Haveyouweighedyourselfinthepastthreemonths4623 = \"Yes\") AND (Smoker = \"Non Smoker\")",
"ActionCmds": [
{
"ActionCmd": "LoadingPercent, TRA, 'LOADING: 100% due to BMI 33 or <38', 100.0000%, '', 568",
"ParentNode": null
},
{
"ActionCmd": "LoadingPercent, DTH, 'LOADING: 50% due to BMI 33 or <38', 50.0000%, '', 569",
"ParentNode": null
}
]
},
{
"Rule": "(Haveyouweighedyourselfinthepastthreemonths4623 = \"Yes\") AND (Smoker = \"Smoker\")",
"ActionCmds": [
{
"ActionCmd": "Outcome, Decline, TRA, 'due to BMI >= 33 & BMI < 38 and Smoker', 1071",
"ParentNode": null
},
{
"ActionCmd": "LoadingPercent, DTH, 'LOADING: 75% due to BMI 33 or <38 and being a smoker', 75.0000%, '', 570",
"ParentNode": null
}
]
},
{
"Rule": "(Haveyouweighedyourselfinthepastthreemonths4623 = \"No\") AND (Smoker = \"Non Smoker\")",
"ActionCmds": [
{
"ActionCmd": "LoadingPercent, TRA, 'LOADING: 100% due to BMI 33 or <38', 100.0000%, '', 568",
"ParentNode": null
},
{
"ActionCmd": "LoadingPercent, DTH, 'LOADING: 50% due to BMI 33 or <38', 50.0000%, '', 569",
"ParentNode": null
}
]
},
{
"Rule": "(Haveyouweighedyourselfinthepastthreemonths4623 = \"No\") AND (Smoker = \"Smoker\")",
"ActionCmds": [
{
"ActionCmd": "Outcome, Decline, TRA, 'due to BMI >= 33 & BMI < 38 and Smoker', 1071",
"ParentNode": null
},
{
"ActionCmd": "LoadingPercent, DTH, 'LOADING: 75% due to BMI 33 or <38 and being a smoker', 75.0000%, '', 570",
"ParentNode": null
}
]
}
],
"Name": "4623",
"Text": "Have you weighed yourself in the past 3 months?",
"HelpText": null,
"InputCaptureRules": {},
"Tag": "Haveyouweighedyourselfinthepastthreemonths4623",
"IsRoot": false,
"IsOptional": false,
"IsLabel": false,
"Note": null,
"HasPlaceHolderInText": false,
"Benefits": [
"DTH",
"TRA"
],
"Style": null,
"IsConfirmationRequired": false,
"Nodes": []
},
{
"Actions": [
{
"Rule": "(Haveyouweighedyourselfinthepastthreemonths4626 = \"Yes\") AND (Smoker = \"Non Smoker\")",
"ActionCmds": [
{
"ActionCmd": "LoadingPercent, DTH, 'LOADING: 75% due to BMI 38 or <39', 75.0000%, '', 573",
"ParentNode": null
}
]
},
{
"Rule": "(Haveyouweighedyourselfinthepastthreemonths4626 = \"Yes\") AND (Smoker = \"Smoker\")",
"ActionCmds": [
{
"ActionCmd": "LoadingPercent, DTH, 'LOADING: 100% due to BMI 38 or <39 and being a smoker', 100.0000%, '', 571",
"ParentNode": null
}
]
},
{
"Rule": "(Haveyouweighedyourselfinthepastthreemonths4626 = \"No\") AND (Smoker = \"Non Smo