Add style and value to json script

Hello all,

I am trying to change a dataset to json dataset with this script, I used this script in script transform section of a data property bininding:

	py_data = system.dataset.toPyDataSet(value)
	data = []
	headers = py_data.getColumnNames()
	row_count = py_data.getRowCount()
	for i in range(row_count):
		row_dict = {}
		for header in headers:
			row_dict[header] = py_data.getValueAt(i, header)
			if headers=='process_value':
				header_dict ={}
		data.append(row_dict)
	return data

As a result,I get this:

image

Now I want to change the column “process_value” to object and be able to add style and value to that object:

So the result should be:

image

How this should be done in the script? I would appreciate your kind help.

First question, just in case: How is your initial dataset (the one refered to by value) obtained ?

Also, what you want is:
From a dataset, that has columns low_limit, high_limit and process_value, make a list of dicts with keys of those same names, but with process_value a dict itself with keys style and value.
Is that correct ?

1 Like

Assuming you can’t get a json instead of a dataset (you can select json as a query output for example),
and that there is no built-in function to convert a dataset into a json,
You could do something like this :

return [
	{
		'low_limit': row[0],
		'high_limit': row[1],
		'process_value': {
			'style': {
				'background_color': "#F7901D",
				'classes': "some_class"
			},
			'value': row[3] 
		}
	} for row in system.dataset.toPyDataSet(value)
]

or directly unpack each row:

return [
	{
		'low_limit': low,
		'high_limit': high,
		'process_value': {
			'style': {
				'background_color': "#F7901D",
				'classes': "some_class"
			},
			'value': proc
		}
	} for low, high, proc in system.dataset.toPyDataSet(value)
]
1 Like

So I created my own dataset using 3 columns and 10 rows. From the dataset standpoint, I do not want to do anything. I would like to be able to change it in JSON as much as possible but if I do not have an option I will switch to the dataset. I want to be able to create a dict (change the process value column to object) so that I can add style and value to it.

So what you are suggesting is that I get json in a custom property and then bind that to another custom property and add the script transform below? Correct?

1 Like

If you want your initial dataset to stay the same, then use that script to transform it into an array of dicts, which is pretty much json.
Just make a property, bind it to your dataset, and add this script transform. This should give you the result you want.

1 Like

This is exactly what I am doing.
So I bind a property to the dataset and added the script transform to change it to JSONPreformatted text and it is working but I do not know how to add style and background to it.

This is my script:

	py_data = system.dataset.toPyDataSet(value)
	data = []
	headers = py_data.getColumnNames()
	row_count = py_data.getRowCount()
	for i in range(row_count):
		row_dict = {}
		for header in headers:
			row_dict[header] = py_data.getValueAt(i, header)
		data.append(row_dict)
	return data

The script in my last post should do it by itself:
It converts value to a pyDataSet then iterates through its elements and unpacks each row into your three variables, and creates a dict in the format you want. Being in a list comprehension, it returns a list containing all the created dicts.
Try replacing your script with this one and see if it’s what you want. If not, tell me what’s wrong and I’ll try to fix it.

1 Like

Dont you already have the code to put in the styles?
why not reuse that? The syntax for dataset or json dict really isnt all that different

1 Like

Thank you very much @pascal.fragnoud
That worked!!! Appreciate your kind help! :slight_smile:

I thought the syntax for JSON should be different as this is the first time I’m using JSON scripting.

I mean, you’re not really using json here, just objects (dicts)

Json is basicly just a string(ified) of such a dict.

You have used it before im sure, ignition(perspective) is full of it!
Like adding in the styles through script is all done with objects, also in the previous script with datasets you used it :slight_smile:

Like this was a simple dict
style_red = {“classes”: “Above”}

1 Like

Yes, you are absoulutely correct!!!
never think of it like that. Thanks for the information @victordcq !