I have data which is serialized as JSON which then has to be added to the Dynamics PluginExeutionContext in a SharedVariable. The Problem is that the JSON content is passed as string and cannot be parsed as JSON on the other side of the receiver.
This is how it is passed now:
"SharedVariables": [
"key": "Telemetry_Log",
"value": "JsonContent"
},
]
What I need is to have the "JsonContent" without double quotes like this
"SharedVariables": [
"key": "Telemetry_Log",
"value": JsonContent
},
]
First I have serialized the data into JSON and passed the string to the context like so:
_executionContext.SharedVariables.Add(TelemetryLogSharedVariables.CustomUserLog.GetDescription(), _loggerContainerUser.ConvertToJson()
second try was to return a list of CRM Entities hoping that Dynamics will serialize it.
the last try was to cast the JSON string to an object:
_executionContext.SharedVariables.Add(TelemetryLogSharedVariables.CustomUserLog.GetDescription(), (Object)_loggerContainerUser.ConvertToJson()
Nothing worked and I always got the JSON string in double quotes.
Does anybody have an advice?
You have to “Create a DataContract class that will define datamodel for JSON data” and deserialize (reverse engineer) the JSON string into Object using DataContractJsonSerializer
on the other end. Read more
Origin: Serialize Data in JSON string
using (MemoryStream SerializememoryStream = new MemoryStream())
{
//create a sample data of type Student Class add details
Student NewStudent = new Student();
NewStudent.FullName = "Sam";
NewStudent.JobTitle = "Developer";
NewStudent.Contact = "808-2125454";
NewStudent.Country = "India";
NewStudent.ZIPCode = "400005";
//initialize DataContractJsonSerializer object and pass Student class type to it
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Student));
//write newly created object(NewStudent) into memory stream
serializer.WriteObject(SerializememoryStream, NewStudent);
//use stream reader to read serialized data from memory stream
StreamReader sr = new StreamReader(SerializememoryStream);
//get JSON data serialized in string format in string variable
string Serializedresult = sr.ReadToEnd();
}
Destination: Deserialize Data obtained in JSON string to an object
using (MemoryStream DeSerializememoryStream = new MemoryStream())
{
//Json String that we get from web api
string ResponseString = "{\"FullName\":\"" + "Sam" + "\",\"JobTitle\":\"" + "Developer" + "\",\"Contact\":\"" + "808-124567" + "\",\"Country\":\"" + "India" + "\",\"ZIPCode\":\"" + "400005" + "\"}";
//initialize DataContractJsonSerializer object and pass Student class type to it
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Student));
//user stream writer to write JSON string data to memory stream
StreamWriter writer = new StreamWriter(DeSerializememoryStream);
writer.Write(ResponseString);
writer.Flush();
DeSerializememoryStream.Position = 0;
//get the Desrialized data in object of type Student
Student SerializedObject = (Student)serializer.ReadObject(DeSerializememoryStream);
}