I am faced with the challenge of merging three JSON strings: JSON-A, JSON-B, and JSON-C. My goal is to combine JSON-A and JSON-B to form JSON-C, but also to split JSON-C back into JSON-A and JSON-B.
Below are the details of the JSONs:
JSON-A: - This contains questions along with all possible answers
{
"content": {
"section": [
{
"questions": [
{
"qText": "Have you or your family received medication or treatment for any serious conditions in the last 2 years?",
"qKey": 152,
"qType": [
{
"aType": "You",
"ans": [
{
"aKey": "102",
"aText": "Yes"
},
{
"aKey": "106",
"aText": "No"
}
]
},
{
"aType": "Your family",
"ans": [
{
"aKey": "108",
"aText": "Yes"
},
{
"aKey": "109",
"aText": "No"
}
]
}
]
}
]
}
]
}
JSON-B: - Selected answers
{
"qkey": "152",
"ans": [
{
"aType": "You",
"aKey": "102"
},
{
"aType": "Your family",
"aKey": "106"
}
]
}
JSON-C: - Output with selectedAnswer
{
"content": {
"section": [
{
"questions": [
{
"qText": "Have you or your family received medication or treatment for any serious conditions in the last 2 years?",
"qKey": 152,
"qType": [
{
"aType": "You",
"selectedAnswer": "102",
"ans": [
{
"aKey": "102",
"aText": "Yes"
},
{
"aKey": "106",
"aText": "No"
}
]
},
{
"aType": "Your family",
"selectedAnswer": "109",
"ans": [
{
"aKey": "108",
"aText": "Yes"
},
{
"aKey": "109",
"aText": "No"
}
]
}
]
}
]
}
]
}
To achieve this result from JSON-A and JSON-B, I implemented multiple nested loops which may appear complex. Is there a more efficient way to perform this task without using jQuery? Any suggestions or alternative methods would be greatly appreciated. Thank you!