Hi I need help below is the json where I have to read and create csv file as output.
{
"COMP_DUR": "100",
"DATE": "2024-01-10T16:38:00Z",
"EMAIL": "[email protected]",
"ID": "1111",
"approval": "Medium",
"criteria": "Medium",
"access": "Neutral"
}
highlighted in yellow colour are json key and values but in green colour are the dynamic json values which i have to map with json key(REQ and RES)
I am doing this as follows
JObject convertedJson = JObject.Parse(json);
IDictionary quesRespkeyValueParam = new Dictionary();
foreach (var item in convertedJson.Properties())
{
//Console.WriteLine(item.Name + "," + item.Value);
if (item.Name == "SURVEY_ID" || item.Name == "RESPONDENT_ID" || item.Name == "RESPONDENT_NAME" || item.Name == "RESPONDENT_EMAIL" ||
item.Name == "DATE_RESPONDED" || item.Name == "COMPLETION_DURATION" || item.Name == "SURVEY_URL")
{
JsonSerializerOptions options = new()
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault
};
}
else
{
quesRespkeyValueParam.Add(item.Name, (string)item.Value);
tempObj.QuesResp = (Dictionary)quesRespkeyValueParam;
}
}
obj = tempObj;I am able to add values in IDictionary object but not able to map with json keys REQ and RES
| COMP_DUR | DATE | ID | REQ | RES | |
| 100 | 2024-01-10T16:38:00Z | [email protected] | 1111 | approval | Medium |
| 100 | 2024-01-10T16:38:00Z | [email protected] | 1111 | criteria | Medium |
| 100 | 2024-01-10T16:38:00Z | [email protected] | 1111 | access | Neutral |
please help. Thanks


Amit MohantyRank #112 · 20K reputationPosted Feb 9, 2024, 9:17 AM EST
Check this
string jsonData = @"{ ""COMP_DUR"": ""100"", ""DATE"": ""2024-01-10T16:38:00Z"", ""EMAIL"": ""[email protected]"", ""ID"": ""1111"", ""approval"": ""Medium"", ""criteria"": ""Medium"", ""access"": ""Neutral"" }"; JObject convertedJson = JObject.Parse(jsonData); string compDur = convertedJson["COMP_DUR"].ToString(); string date = convertedJson["DATE"].ToString(); string email = convertedJson["EMAIL"].ToString(); string id = convertedJson["ID"].ToString(); var dynamicProps = convertedJson.Properties().Where(p => p.Name != "COMP_DUR" && p.Name != "DATE" && p.Name != "EMAIL" && p.Name != "ID"); Console.WriteLine("COMP_DUR,DATE,EMAIL,ID,REQ,RES"); foreach (var prop in dynamicProps) { Console.WriteLine($"{compDur},{date},{email},{id},{prop.Name},{prop.Value}"); }