[BUG-15901] JSON encoding and decoding a "Dataset" datatype property

I am trying to pass a value of “Dataset” type as a value in a dictionary which I need to store in a “String” format. Assume the following:

dataset: variable of type “Dataset”
dict = {‘set’ : dataset}

To build the string representation for the dictionary, I am using system.util.jsonEncode. When I try to retrieve the dictionary from its string representation using system.util.jsonDecode, I do not get the dataset back in “Dataset” type. Instead, I get a representation of it in some form.

Is there a way to get back the dataset from its representation? If not, is there any other way to achieve the following: Dataset --> Dictionary --> String --> Dictionary --> Dataset?

I was able to do so with a few extra steps. It basically goes Dataset -> Encoded dataset -> Dictionary -> String -> Dictionary -> Encoded dataset -> Dataset. In the example here, I am reading a dataset tag, but it should work with any other dataset given you first convert it to a pydataset.

	#Original Dataset
	dataset = system.tag.readBlocking(['asdf'])[0].value
	#Convert to Pydata
	pydata = system.dataset.toPyDataSet(dataset)
	#Encode JUST the dataset
	encoded_dataset = system.util.jsonEncode(pydata)
	#Create the Dict
	some_dict = {'the_data':encoded_dataset}
	#Encode the Dict with the encoded data inside
	encoded_dict = system.util.jsonEncode(some_dict)
	#Decode the dictionary
	decoded_dict = system.util.jsonDecode(encoded_dict)
	#Decode the inner dataset
	self.custom.dataset = system.util.jsonDecode(decoded_dict.get('the_data'))

All this said, this should probably be improved on our end.

You can import and use TypeUtilities; something like this should work:

from com.inductiveautomation.ignition.common import TypeUtilities
jsonStr = str(TypeUtilities.datasetToJSON(<your dataset>))
ds = TypeUtilities.datasetFromJSON(jsonStr)

Thanks @osolorzano. That worked but I agree with you, an improvement here can be quite helpful.

Thanks for your response but the solution did not work. When trying to get the dataset from JSON string, it errors out as the json string could not be coerced to JSON.

The reason Paul’s instruction threw an error is the decode command should be:

ds = TypeUtilities.toDataset(jsonStr)

The advantage of using TypeUtilities is that you can pass in a “real” dataset which returns a “real” dataset instead of a pythonDataset.

how about this? create a json dict in a project library script to be call by the ‘runScript()’ expression function then to use the derived tag to push the json to a dataset memory tag? using jsonGet and Set?

@PGriffith Paul, I am using:


to convert a dataset to a string for messaging. This works fine until the message contains unusual characters. For instance, if I try to send the Swedish word “kalenderår” it gets corrupted to “kalenderÃ¥r”.
Any ideas?

str is collapsing to a lower character encoding. Try using unicode() around the TypeUtilities call.

Thank you. Thank you. Thank you. I love easy fixes.

1 Like