Question

How to deserialize JSON to a Dictionary object in a Script task?

I need to deserialize a JSON string in a process script task.  The JSON has to be serialized into a Dictionary object as it is not possible to define a class in a Script task.

 

Using `Terrasoft.Common.Json`:

var recordJSON = Get<string>("ProcessSchemaParameterRecordJSON");
var recordDict = Json.DeserializeDictionary(recordJSON);
var recordStr = recordDict["accountName"];
Set<string>("ProcessSchemaParameterRecordText", recordStr);
return true;

Results in the error:

System.NullReferenceException: Object reference not set to an instance of an object.
   at Terrasoft.Common.Json.DictionaryJsonConverter.DeserializeDictionaryItem(JsonReader reader)
   at Terrasoft.Common.Json.DictionaryJsonConverter.ReadJson(JsonReader reader, Type objectType, Object existingValue, JsonSerializer serializer)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.DeserializeConvertable(JsonConverter converter, JsonReader reader, Type objectType, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
   at Terrasoft.Common.Json.Json.DeserializeDictionary(String source)

`Newtonsoft.Json` looked promising:

JsonConvert.DeserializeObject<Dictionary<string, string>>(recordJSON);

However the parser threw an error when it came up against an array in the JSON.

`System.Text.Json` does not support deserializing to a dictionary object as far as I can see.

The `System.Web.Script` namespace does not appear to be available in Creatio.

Thanks in advance,

Like 0

Like

2 comments

Please study the example I created for your question in the https://community.creatio.com/questions/help-parse-json-i-get-webservic… thread. It should help in your case either.

Thank you for that Oleg, much appreciated.

Show all comments