-1

I have the requirement as follows. The Json needs to look like this:

  "facilityId": "{{facilityId}}",
  "apiDate": {{apiDate}},
  "apiSignature": "{{apiSignature}}",
  "surveys": [
    {
      "sessionId": "sesh1",
      "instrumentId": "DEMO",
      "clientId": "a100",
      "assignedToType": "Client",
      "completeDate": "2/3/19",
      "answerStyle": "byValue",
      "Q1": "This is a long test text message of the testering.",
      "Q2": "2",
      "Q3": "2|3|1",
      "Q4": 9
    },
    {
      "sessionId": "sesh2",
      "instrumentId": "DEMO",
      "clientId": "a101",
      "assignedToType": "Client",
      "completeDate": "20201007",
      "answerStyle": "byText",
      "Q1": "Green",
      "Q2": "Sometimes",
      "Q3": "Agree",
      "Q4": 9
    }
  ]
}

I need the Q1,Q2,Q3....ect.... to be a dictionary since its a dynamic amount of questions.

But anytime I use a dictionary, I get the following from it....

"surveyAnswers": 
{
      "Q1": "Green",
      "Q2": "Sometimes",
      "Q3": "Agree",
      "Q4": 9
}

I need it to flatten and remove "surveyAnswers"

I have tried all of the flattening techniques on here but that changes it to this


      "surveyAnswers.Q1": "Green",
      "surveyAnswers.Q2": "Sometimes",
      "surveyAnswers.Q3": "Agree",
      "surveyAnswers.Q4": 9
}

How can I just flatten the dictionary to not include the name of the dictionary and just appear to be another property of my object.

I hope that makes sense.

Thanks

Garen
  • 69
  • 4
  • 1
    Please edit your question to show how you use the dictionary and where you get the wrong value from – Sorashi Sep 21 '21 at 18:05
  • 1
    I think that this question and answer covers the same topic: https://stackoverflow.com/a/45347942/1398417 – Kristofer Sep 21 '21 at 18:11

1 Answers1

1

After much more searching and rewording my question. I found the answer here.

Is it possible to flatten (hide) a Dictionary variable name?

The solution was to change my dictionary from

a Dictionary<string, string> to a Dictionary<string, Jtoken>

Then I applied the [JsonExtensionData] tag on it and the rest worked flawlessly.

I am very satified with the result becuase now my Json looks like the following

            "clientId": "Test1234",
            "assignedTotype": 0,
            "completedDate": "20210921",
            "answerStyle": 1,
            "Q1": "hello",
            "Q2": "goodbye"
        }

Exactly what I wanted. Thanks again and hope this helps someone else.

Garen
  • 69
  • 4