# Converting dataset to json HELP

**URL:** <https://forum.inductiveautomation.com/t/converting-dataset-to-json-help/60311>\
**Category:** General Discussion\
**Created:** [June 2, 2022, 4:15pm UTC](https://forum.inductiveautomation.com/t/converting-dataset-to-json-help/60311 "2022-06-02T16:15:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mjohnson0238](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@mjohnson0238](https://forum.inductiveautomation.com/u/mjohnson0238)\
**Post date:** [June 2, 2022, 4:15pm UTC](https://forum.inductiveautomation.com/t/converting-dataset-to-json-help/60311/1 "2022-06-02T16:15:01Z")

</div>

I am in need of help, I have a dataset that i need to make a json.

This is the dataset:  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/d/1df1b7ad69a72f7adecd02380f582a7b35a8a460.png)

How can i convert it to a json so i can parse column data?

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [June 2, 2022, 4:29pm UTC](https://forum.inductiveautomation.com/t/converting-dataset-to-json-help/60311/2 "2022-06-02T16:29:59Z")

</div>

> [@Looping through JSON](https://forum.inductiveautomation.com/t/looping-through-json/53900/5):
>
> Don’t use ‘List’ as a variable name. This is a terrible practice even if you know it’s wrong and you’re only writing test code. Just kick the habit of using python built in names as variables, so nothing like list,dict,str,int,tuple,set,float should ever be used as variable names. And there are many others besides those You can enumerate the instances and add the index to them in the for loop with the enumerate() function. Something like this should do: instances = [ { 'mes\_spec':…

---

<div class="post-metadata">

**Author:** ![mjohnson0238](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@mjohnson0238](https://forum.inductiveautomation.com/u/mjohnson0238)\
**Post date:** [June 2, 2022, 6:02pm UTC](https://forum.inductiveautomation.com/t/converting-dataset-to-json-help/60311/3 "2022-06-02T18:02:22Z")

</div>

Thank you sir

---

<div class="post-metadata">

**Author:** ![mjohnson0238](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@mjohnson0238](https://forum.inductiveautomation.com/u/mjohnson0238)\
**Post date:** [June 2, 2022, 9:19pm UTC](https://forum.inductiveautomation.com/t/converting-dataset-to-json-help/60311/4 "2022-06-02T21:19:49Z")

</div>

Anyway I can insert rows into this? I would like to add Denier rows with names in the column. Then have a Doff\_Number default value of 0 on each row, then when something in that row it replaces that 0 with a value?

```auto
	columnnames = [
		{
			'Doff_Number':Doff_Number,
			'Denier':Denier,
		} for Doff_Number,Denier in system.dataset.toPyDataSet(value)
	]
	
	return columnnames

```

So it would resemble something like this:  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/d/d/dd9ddb0cbd992243537fc330d2499fc9940310a9.png)

Then if only the 1385 row had doff\_number data it will fill it in but leave the rest at 0, like this:  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/6/96d62de05b6bceb0898a49214a33059cf14446be.png)

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [June 2, 2022, 9:42pm UTC](https://forum.inductiveautomation.com/t/converting-dataset-to-json-help/60311/5 "2022-06-02T21:42:52Z")

</div>

```auto
columnnames = [
		{
			'Doff_Number':Doff_Number if Doff_Number else 0,
			'Denier':Denier,
		} for Doff_Number,Denier in system.dataset.toPyDataSet(value)
	]
	
	return columnnames

```

I think this will get you what you’re looking for, but it’s sort of dangerous depending on the type of the column.

For example, if `Doff_Number` is a string column, and a row contains a value of `"0"` in that column, then you’ll get String “0” instead of `0` because `bool("0")` equates to `True`. So you need to be aware of what type a given column is.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [February 15, 2023, 1:29am UTC](https://forum.inductiveautomation.com/t/converting-dataset-to-json-help/60311/6 "2023-02-15T01:29:39Z")

</div>

Just for posterity, below is useable for converting _any_ dataset into ~~a json object~~ Py objects. Of course, it doesn't provide the extra condition in the OP, but this could be added with an if

```auto
pds = system.dataset.toPyDataSet(value)
columnNames = pds.columnNames 
ret = [
	{
		col: row[col]
		for col in columnNames
	} for row in pds
]

return ret

```

---

<div class="post-metadata">

**Author:** ![Luis\_Perez1](https://avatars.discourse-cdn.com/v4/letter/l/b3f665/32.png) [@Luis\_Perez1](https://forum.inductiveautomation.com/u/Luis_Perez1)\
**Post date:** [August 14, 2023, 5:29pm UTC](https://forum.inductiveautomation.com/t/converting-dataset-to-json-help/60311/7 "2023-08-14T17:29:23Z")

</div>

this worked like a charm. thanks!
