# Add style and value to json script

**URL:** <https://forum.inductiveautomation.com/t/add-style-and-value-to-json-script/52158>\
**Category:** Ignition\
**Created:** [October 20, 2021, 2:25pm UTC](https://forum.inductiveautomation.com/t/add-style-and-value-to-json-script/52158 "2021-10-20T14:25:24Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [October 20, 2021, 2:25pm UTC](https://forum.inductiveautomation.com/t/add-style-and-value-to-json-script/52158/1 "2021-10-20T14:25:24Z")

</div>

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:

```auto
	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](https://us1.discourse-cdn.com/inductiveautomation/original/3X/5/e/5e2a765a1639d53b40957dd4128f5bc3ada80f1d.png)

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](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/7/67782d54a327865ae3c54a8a3779ef77cad33c8f.png)

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

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [October 20, 2021, 3:21pm UTC](https://forum.inductiveautomation.com/t/add-style-and-value-to-json-script/52158/2 "2021-10-20T15:21:00Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [October 20, 2021, 3:35pm UTC](https://forum.inductiveautomation.com/t/add-style-and-value-to-json-script/52158/3 "2021-10-20T15:35:15Z")

</div>

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 :

```python
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:

```python
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)
]

```

---

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [October 20, 2021, 4:58pm UTC](https://forum.inductiveautomation.com/t/add-style-and-value-to-json-script/52158/4 "2021-10-20T16:58:52Z")

</div>

> [@pascal.fragnoud](#):
>
> How is your initial dataset (the one refered to by `value` ) obtained

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?

> [@pascal.fragnoud](#):
>
> ```auto
> 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)
> ]
> 
> ```

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [October 21, 2021, 8:06am UTC](https://forum.inductiveautomation.com/t/add-style-and-value-to-json-script/52158/5 "2021-10-21T08:06:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [October 21, 2021, 12:17pm UTC](https://forum.inductiveautomation.com/t/add-style-and-value-to-json-script/52158/6 "2021-10-21T12:17:13Z")

</div>

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

This is my script:

```auto
	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

```

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [October 21, 2021, 12:35pm UTC](https://forum.inductiveautomation.com/t/add-style-and-value-to-json-script/52158/7 "2021-10-21T12:35:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![victordcq](https://avatars.discourse-cdn.com/v4/letter/v/f6c823/32.png) [@victordcq](https://forum.inductiveautomation.com/u/victordcq)\
**Post date:** [October 21, 2021, 12:49pm UTC](https://forum.inductiveautomation.com/t/add-style-and-value-to-json-script/52158/8 "2021-10-21T12:49:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [October 21, 2021, 1:03pm UTC](https://forum.inductiveautomation.com/t/add-style-and-value-to-json-script/52158/9 "2021-10-21T13:03:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [October 21, 2021, 1:04pm UTC](https://forum.inductiveautomation.com/t/add-style-and-value-to-json-script/52158/10 "2021-10-21T13:04:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![victordcq](https://avatars.discourse-cdn.com/v4/letter/v/f6c823/32.png) [@victordcq](https://forum.inductiveautomation.com/u/victordcq)\
**Post date:** [October 21, 2021, 1:17pm UTC](https://forum.inductiveautomation.com/t/add-style-and-value-to-json-script/52158/11 "2021-10-21T13:17:34Z")

</div>

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 🙂

Like this was a simple dict  
style\_red = {“classes”: “Above”}

---

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [October 21, 2021, 1:26pm UTC](https://forum.inductiveautomation.com/t/add-style-and-value-to-json-script/52158/12 "2021-10-21T13:26:21Z")

</div>

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