I have been stuck on this for about a week now. I am receiving a json response to my API as follows, it consists of question IDs(32 character long string) and the related answers, for some questions, the answers are predefined hence some answers has IDs as well. Depend on the question there could be multiple answers or one answer.
{
"b07e33061bd31c8d29095f44d9898826": {
"answer": "abc"
},
"c4edd6e206973168fb15ced212397197": {
"answer": "def"
},
"f35270e30bafb94a30f1b22786f748a6": {
"selectedAnswers": [
"b66c043042586e893a147d4b0ba53bdf",
"85eb345f9ad8035bb8928faa4b2b741d",
"071475576d1925e595c39e47ea00e45c"
]
},
"fc6b41df07db039e3903665e0669f8e9": {
"58ff182f96dd321a24bcd08a982566c6": "11b5da0633d22d584f58f85c21804dbf",
"6fee5fc87f6515467f870015b84d8975": "5467a55ce17aa356898592a60d06964e",
"7281d97f90af65eae568320ce3b1e510": "59e7d9c42190c6882d28c8705c2b3cca",
"a69fe0b53807a01ff8f0c83069c74ecf": "92323316ddacdd7e0db911e12ee9ec20",
"d4e0e900c9c960a9b8cd994ea984c7d6": "dfe0109763b30f5be4055c1097be6648"
},
"60ed2fc37d5207b03ad2a72c6ae56793": {
"answer": "hij"
},
"593dfbd2317debd60e74c321512fe77a": {
"1c99416b1c016fdf0ce0b7c996e402e8": "0e1c73a2846468eef95313d4e5c394d6",
"aabd5d7ceebf0ca04970cf836f8aaa41": "4edea9bc2acd426c04b20ad0f656dfda",
"df9b926b795e8ec31bef4156435c4ab9": "aa17bd8932f47b26caf8bd27aa8b00e9"
},
"fcb5de7c3484c88120c92edf399d17a8": {
"answer": "klm"
},
"0f9d2977e66fe7e6bcfb78659f13f9af": {
"answer": "nop"
},
"92de1c7bae914e1213ecc95dd0a7c8a0": {
"answer": "qrs"
},
"74e7f471011fdbf780f25563f4f92a0b": {
"answer": "tuv"
},
"75fa3e245138f7fadc68083aebab55c2": {
"answer": 5
},
"e41bb071c73d64647e65f1474a12604b": {},
"year": 2019,
"quarter": 1
}
Some questions has sub questions inside of it. Like in this case.
"593dfbd2317debd60e74c321512fe77a": {
"1c99416b1c016fdf0ce0b7c996e402e8": "0e1c73a2846468eef95313d4e5c394d6",
"aabd5d7ceebf0ca04970cf836f8aaa41": "4edea9bc2acd426c04b20ad0f656dfda",
"df9b926b795e8ec31bef4156435c4ab9": "aa17bd8932f47b26caf8bd27aa8b00e9"
}
I need it to be processed and converted in to a key value pair so i can store it in a database having a structure like this.
+----+------+---------+----------------------------------+----------------------------------+
| Id | Year | Quarter | Question | Answer |
+----+------+---------+----------------------------------+----------------------------------+
| 1 | 2019 | 1 | b07e33061bd31c8d29095f44d9898826 | abc |
| 2 | 2019 | 1 | c4edd6e206973168fb15ced212397197 | def |
| 3 | 2019 | 1 | f35270e30bafb94a30f1b22786f748a6 | b66c043042586e893a147d4b0ba53bdf |
| 4 | 2019 | 1 | f35270e30bafb94a30f1b22786f748a6 | 85eb345f9ad8035bb8928faa4b2b741d |
| 5 | 2019 | 1 | f35270e30bafb94a30f1b22786f748a6 | 071475576d1925e595c39e47ea00e45c |
| 6 | 2019 | 1 | fc6b41df07db039e3903665e0669f8e9 | null |
| 7 | 2019 | 1 | 58ff182f96dd321a24bcd08a982566c6 | 11b5da0633d22d584f58f85c21804dbf |
| 8 | 2019 | 1 | 6fee5fc87f6515467f870015b84d8975 | 5467a55ce17aa356898592a60d06964e |
+----+------+---------+----------------------------------+----------------------------------+
To make things simpler I will summarize the question IDs further.
+----+------+---------+--------------+------------+
| Id | Year | Quarter | Question | Answer |
+----+------+---------+--------------+------------+
| 1 | 2019 | 1 | questionId 1 | abc |
| 2 | 2019 | 1 | questionId 2 | def |
| 3 | 2019 | 1 | questionId 3 | answerId 1 |
| 4 | 2019 | 1 | questionId 3 | answerId 2 |
| 5 | 2019 | 1 | questionId 3 | answerId 3 |
| 6 | 2019 | 1 | questionId 4 | null |
| 7 | 2019 | 1 | questionId 5 | answerId 4 |
| 8 | 2019 | 1 | questionId 6 | answerId 5 |
+----+------+---------+--------------+------------+
So I decided to create a new json model with the structure of,
"year" : "2019"
"quarter" : "1"
"question 1" : "answer 1"
"question 2" : "answer 2"
"question 3" : "answer 3"
Currently I have tried to loop through the object and get the values but this algorithm is too complex and consumes time. In some cases it do not properly match answers with questions.
public async Task<IActionResult> PostResponses([FromBody] dynamic jsonObject)
{
string answerText = null;
var model = new JObject();
model.Add("Year", jsonObject["year"].ToString());
model.Add("Quarter", jsonObject["quarter"].ToString());
using (var reader = new JsonTextReader(new StringReader("[" + jsonObject + "]")))
{
while (reader.Read())
{
string questionText = null;
if (reader.TokenType == JsonToken.PropertyName )
{
string questionId = reader.Value.ToString();
if (questionId.Length == 32)
{
try
{
// get question corresponding to this question ID
var question = await _context.Questions.FirstOrDefaultAsync(s => s.Id == questionId);
System.Diagnostics.Debug.WriteLine("Question -" + questionId);
questionText = question.Text;
}
catch (Exception)
{
}
}
}
if (reader.TokenType == JsonToken.String || reader.TokenType == JsonToken.Integer)
{
string answerId = reader.Value.ToString();
if (answerId.Length == 32)
{
try
{
// get answer corresponding to this answer ID
var answers = await _context.OfferedAnswers.FirstOrDefaultAsync(s => s.Id == answerId);
answerText = answers.Value;
}
catch (Exception)
{
}
}
else
{
answerText = answerId;
}
}
if (questionText != null && answerText != null)
{
model.Add(questionText, answerText);
}
}
System.Diagnostics.Debug.WriteLine(model.ToString());
}
return Ok();
}
If someone can suggest a better way to do this is highly appreciated. I see no other way of doing this. Thank you in advance.