# Load JSON file and convert to dataset

**URL:** <https://forum.inductiveautomation.com/t/load-json-file-and-convert-to-dataset/68518>\
**Category:** Ignition\
**Created:** [January 11, 2023, 10:51pm UTC](https://forum.inductiveautomation.com/t/load-json-file-and-convert-to-dataset/68518 "2023-01-11T22:51:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrutten](https://avatars.discourse-cdn.com/v4/letter/m/c0e974/32.png) [@mrutten](https://forum.inductiveautomation.com/u/mrutten)\
**Post date:** [January 11, 2023, 10:51pm UTC](https://forum.inductiveautomation.com/t/load-json-file-and-convert-to-dataset/68518/1 "2023-01-11T22:51:49Z")

</div>

I am trying to load a JSON file and convert it into a dataset. I provided a smaller portion of the json file. All I want to do is Load the file and show it in a datatable. I looked at a lot of documentation but could not find anything that works for this.  
[  
{  
"CONFIGURABLEITEMID": 55,  
"VALUE": "DIA",  
"Location": "Waco",  
"Environment": "Production",  
"Schema": "UTWSHP"  
},  
{  
"CONFIGURABLEITEMID": 56,  
"VALUE": "C:\NetApp\OMS",  
"Location": "Waco",  
"Environment": "Production",  
"Schema": "UTWSHP"  
}  
]

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [January 11, 2023, 11:04pm UTC](https://forum.inductiveautomation.com/t/load-json-file-and-convert-to-dataset/68518/2 "2023-01-11T23:04:48Z")

</div>

One possible way to do it, assuming you're starting from a true JSON string:

```python
content = """[
{
"CONFIGURABLEITEMID": 55,
"VALUE": "DIA",
"Location": "Waco",
"Environment": "Production",
"Schema": "UTWSHP"
},
{
"CONFIGURABLEITEMID": 56,
"VALUE": "C:\\\NetApp\\\OMS",
"Location": "Waco",
"Environment": "Production",
"Schema": "UTWSHP"
}
]"""

def jsonToDataset(content):
	import json
	raw = iter(json.loads(content))

	firstRow = next(raw)
	headers = sorted(firstRow.keys())
	data = [[firstRow[key] for key in headers]]
	for row in raw:
		data.append([row[key] for key in headers])

	return system.dataset.toDataSet(headers, data)
	
ds = jsonToDataset(content)

```

This makes certain assumptions (your JSON document will never be more deeply nested, the first object always has all the keys you care about, you don't care about the order of columns in the output), but it should be a solid starting point.

---

<div class="post-metadata">

**Author:** ![mrutten](https://avatars.discourse-cdn.com/v4/letter/m/c0e974/32.png) [@mrutten](https://forum.inductiveautomation.com/u/mrutten)\
**Post date:** [January 12, 2023, 3:18pm UTC](https://forum.inductiveautomation.com/t/load-json-file-and-convert-to-dataset/68518/3 "2023-01-12T15:18:05Z")

</div>

Paul,

Thank you.

When I run this in script console it works fine.

But when I have a window and add the same code to a button, it fails?

 ![](https://us1.discourse-cdn.com/inductiveautomation/original/3X/4/9/4985e5bc75c107aece3d26ea371c8586dda5bd89.png)

Button code:

**def** jsonToDataset(content):  
**import** json  
raw = iter(json.loads(content))

firstRow = next(raw)  
headers = sorted(firstRow.keys())  
data = [[firstRow[key] **for** key **in** headers]]  
**for** row **in** raw:  
data.append([row[key] **for** key **in** headers])

**return** system.dataset.toDataSet(headers, data)

content = system.file.readFileAsString("C:\UT Schemas\test.json")  
**print** content  
ds = jsonToDataset(content)  
**print** ds  
**print** ds.getColumnAsList(0)  
**print** ds.getColumnNames()  
_#event.source.parent.getComponent('Table').data = ds_

Button error:

 ![](https://us1.discourse-cdn.com/inductiveautomation/original/3X/5/2/52d0e4c6058a9567f4be4f94dfa645766c91d6f7.png)

Marcel.

---

<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:** [January 12, 2023, 3:57pm UTC](https://forum.inductiveautomation.com/t/load-json-file-and-convert-to-dataset/68518/4 "2023-01-12T15:57:13Z")

</div>

did you recently change files in user-lib? or pylib  
ifso restart your gateway,

---

<div class="post-metadata">

**Author:** ![mrutten](https://avatars.discourse-cdn.com/v4/letter/m/c0e974/32.png) [@mrutten](https://forum.inductiveautomation.com/u/mrutten)\
**Post date:** [January 12, 2023, 5:27pm UTC](https://forum.inductiveautomation.com/t/load-json-file-and-convert-to-dataset/68518/5 "2023-01-12T17:27:53Z")

</div>

That did it. Thank you very much.  
Now it is working correctly.

Here is the solution with the help from Paul and Victor.

I created a new window.  
added a button and a table

Code I put in the button.  
content = system.file.readFileAsString("C:\UT Schemas\test.json")

def jsonToDataset(content):  
import json  
raw = iter(json.loads(content))

```
firstRow = next(raw)
headers = sorted(firstRow.keys())
data = [[firstRow[key] for key in headers]]
for row in raw:
	data.append([row[key] for key in headers])

return system.dataset.toDataSet(headers, data)

```

ds = jsonToDataset(content)  
print ds  
print ds.getColumnAsList(0)  
print ds.getColumnNames()  
event.source.parent.getComponent('Table').data = ds

Screen shot.  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/1/814004373b705365dd473edc09512977276a6cec.png)
