# To convert JSON data into Dataset form

**URL:** <https://forum.inductiveautomation.com/t/to-convert-json-data-into-dataset-form/83487>\
**Category:** Ignition\
**Tags:** database, scripting, vision\
**Created:** [December 23, 2023, 3:59am UTC](https://forum.inductiveautomation.com/t/to-convert-json-data-into-dataset-form/83487 "2023-12-23T03:59:19Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sumeet\_saibi](https://avatars.discourse-cdn.com/v4/letter/s/7ea924/32.png) [@Sumeet\_saibi](https://forum.inductiveautomation.com/u/Sumeet_saibi)\
**Post date:** [December 23, 2023, 3:59am UTC](https://forum.inductiveautomation.com/t/to-convert-json-data-into-dataset-form/83487/1 "2023-12-23T03:59:19Z")

</div>

How to Convert JSON data into a Dataset form to add in Table.

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [December 23, 2023, 12:01pm UTC](https://forum.inductiveautomation.com/t/to-convert-json-data-into-dataset-form/83487/2 "2023-12-23T12:01:48Z")

</div>

There are several approaches to this that have been developed in this recent thread:  
[JSON to Ignition Dataset](https://forum.inductiveautomation.com/t/json-to-ignition-dataset/82274/4)

They all work, so I suppose which one you choose will depend on what flavor of convolution your development team prefers.

---

<div class="post-metadata">

**Author:** ![Sumeet\_saibi](https://avatars.discourse-cdn.com/v4/letter/s/7ea924/32.png) [@Sumeet\_saibi](https://forum.inductiveautomation.com/u/Sumeet_saibi)\
**Post date:** [December 25, 2023, 10:26am UTC](https://forum.inductiveautomation.com/t/to-convert-json-data-into-dataset-form/83487/3 "2023-12-25T10:26:40Z")

</div>

how to solve this error

Traceback (most recent call last):  
File "", line 9, in   
KeyError: 'results'

---

<div class="post-metadata">

**Author:** ![Sumeet\_saibi](https://avatars.discourse-cdn.com/v4/letter/s/7ea924/32.png) [@Sumeet\_saibi](https://forum.inductiveautomation.com/u/Sumeet_saibi)\
**Post date:** [December 25, 2023, 10:35am UTC](https://forum.inductiveautomation.com/t/to-convert-json-data-into-dataset-form/83487/4 "2023-12-25T10:35:41Z")

</div>

This is my json code. The same type data is going to be added after certain interval.

```auto
{"device_id": "f8f0056d6be3","device_type": "Disinfection","time_date": 
1702309720,"firmware_version": "v2.21j_Quad_DM_WFI, Board r0","pressure_in": 
20,"pressure_concentrate": 0,"pressure_out": 0,"gal_total_outlet": 
623930,"flow_inlet_gpm": 0.00,"flow_outlet_gpm": 0.00,"tds_inlet": 
396,"tds_outlet": 76,"temp_inlet": 70.8,"pump_on": 1,"solenoid_open": 
1,"pct_rem_prefilter": 0,"pct_rem_membrane": 83,"pct_rem_aux_pre": 
0,"pct_rem_aux_post": 0,"total_use_prefilter": 218243,"total_use_membrane": 
166269,"total_use_aux_pre": 218243,"total_use_aux_post": 
166269,"days_rem_aux_pre": 0,"days_rem_prefilter": 0,"days_rem_membrane": 
1427,"days_rem_aux_post": 0,"usage_est_daily_outlet": 584,"max_gal_prefilter": 
100000,"max_gal_membrane": 1000000,"max_gal_aux_pre": 
100000,"max_gal_aux_post": 60000,"recovery_pct": 79,"rpm_set": 
2300,"rpm_meas": 0,"pressure_pump_in": 20,"temp_eBox": 16.8,"temp_motor": 
183.4,"valves": "11-0010-0","resist_12": 0.00,"resist_memb": 0.00,"effic": 
"Std","tds_inlet_contin": 396,"tds_outlet_contin": 76,"outlet_resist": 
0.25,"UV_irradiance": 2.81,"UV_dosage": 999.90,"UV_bulb_days_rem": 
0,"status_msg": "CHECK OUTLET FLOWMETER","outlet_flow_sensor_failure": 
1,"solenoid_driver_fault": 1,"alert_UV_irradiance": 1,"pump_hrs": 173.2} **strong text**

```

---

<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:** [December 25, 2023, 11:07am UTC](https://forum.inductiveautomation.com/t/to-convert-json-data-into-dataset-form/83487/5 "2023-12-25T11:07:35Z")

</div>

> [@Sumeet\_saibi](#):
>
> how to solve this error
> 
> Traceback (most recent call last):  
> File "", line 9, in  
> KeyError: 'results'

As much as we like to think we're magicians some of the time, we don't know which code you decided to use, nor what line 9 is. Although that error is pretty easy to decipher. The code is trying to get a "results" key from an object (dict?) and it doesn't exist in the dict keys

---

<div class="post-metadata">

**Author:** ![Sumeet\_saibi](https://avatars.discourse-cdn.com/v4/letter/s/7ea924/32.png) [@Sumeet\_saibi](https://forum.inductiveautomation.com/u/Sumeet_saibi)\
**Post date:** [December 25, 2023, 11:37am UTC](https://forum.inductiveautomation.com/t/to-convert-json-data-into-dataset-form/83487/6 "2023-12-25T11:37:31Z")

</div>

I used the code from here.  
I just Copy pasted it.  
[https://forum.inductiveautomation.com/t/json-to-ignition-dataset/82274/4](https://forum.inductiveautomation.com/t/json-to-ignition-dataset/82274/4)

---

<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:** [December 25, 2023, 11:59am UTC](https://forum.inductiveautomation.com/t/to-convert-json-data-into-dataset-form/83487/7 "2023-12-25T11:59:30Z")

</div>

That code reads in a json file from a file on the gateway. Is that what you want to do?

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [December 25, 2023, 12:54pm UTC](https://forum.inductiveautomation.com/t/to-convert-json-data-into-dataset-form/83487/8 "2023-12-25T12:54:33Z")

</div>

> [@nminchin](#):
>
> reads in a json file from a file on the gateway

Not necessarily. If this is Vision, and a valid file path is supplied, it will read the file from the client. It's Christmas where I live, so I won't have time to look at this today.

---

<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:** [December 25, 2023, 10:53pm UTC](https://forum.inductiveautomation.com/t/to-convert-json-data-into-dataset-form/83487/9 "2023-12-25T22:53:05Z")

</div>

Ah yes, I didn't see the vision tag!

Still, if this is all he did

> [@Sumeet\_saibi](#):
>
> I just Copy pasted it.

Then it won't work either way. It needs the file path to work

---

<div class="post-metadata">

**Author:** ![Sumeet\_saibi](https://avatars.discourse-cdn.com/v4/letter/s/7ea924/32.png) [@Sumeet\_saibi](https://forum.inductiveautomation.com/u/Sumeet_saibi)\
**Post date:** [December 26, 2023, 4:17am UTC](https://forum.inductiveautomation.com/t/to-convert-json-data-into-dataset-form/83487/10 "2023-12-26T04:17:11Z")

</div>

Thank You for your time. I will provide detailed question.  
Happy Christmas!

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [December 27, 2023, 8:06pm UTC](https://forum.inductiveautomation.com/t/to-convert-json-data-into-dataset-form/83487/11 "2023-12-27T20:06:31Z")

</div>

The error is occurring is because the example JSON doesn't use a 'results' key. That said, the linked dataset converter also assumes that the JSON file will have more than one row of data, or perhaps better stated: a nested data structure. Consequently, the copy and pasted code would also fail for this use case since this JSON is just flat list of key value pairs.

To create an initial dataset using this file, the code would have to be simplified to look like this:

```auto
filepath = 'C:/example/file/path/rowData.json'

# Get the file as a string
jsonString = system.file.readFileAsString(filepath)

# Load the file as a json object
jsonFile = system.util.jsonDecode(jsonString)

# Get the headers from the first key's keys
headers = jsonFile.keys()

# Initialize a data list for or the row lists that will comprise the data in the dataset
data = [[jsonFile[header] for header in headers]]

# Convert the headers and data into a dataset
dataset = system.dataset.toDataSet(headers, data)

```

**Result:**

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/d/4/d4210df9dfba4619e75a9355d36dee2942481b28.png)

> [@Sumeet\_saibi](#):
>
> The same type data is going to be added after certain interval.

To add a row to an existing dataset in a table using a single row file, the code will look like this:

```auto
filepath = 'C:/example/file/path/rowData.json'

# Get the file as a string
jsonString = system.file.readFileAsString(filepath)

# Load the file as a json object
jsonFile = system.util.jsonDecode(jsonString)

# Get the headers from the first key's keys
headers = jsonFile.keys()

# Get the existing dataset from whereever it is stored
dataset = event.source.parent.getComponent('Power Table').data

# Get the next row from the flat JSON file
row = [jsonFile[header] for header in headers]

# Create a new dataset by adding a row to the old one
newDataset = system.dataset.addRow(dataset, dataset.rowCount, row)

# Replace the old dataset with the new one
event.source.parent.getComponent('Power Table').data = newDataset

```

**Result:**

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/a/5/a5b1cff04abbbb3b18dbc7bf3043170e96b98193.png)
