# Convert Named query into an array

**URL:** <https://forum.inductiveautomation.com/t/convert-named-query-into-an-array/78696>\
**Category:** Ignition\
**Tags:** scripting\
**Created:** [September 3, 2023, 2:34pm UTC](https://forum.inductiveautomation.com/t/convert-named-query-into-an-array/78696 "2023-09-03T14:34:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Morgan1](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@John\_Morgan1](https://forum.inductiveautomation.com/u/John_Morgan1)\
**Post date:** [September 3, 2023, 2:34pm UTC](https://forum.inductiveautomation.com/t/convert-named-query-into-an-array/78696/1 "2023-09-03T14:34:03Z")

</div>

[https://docs.inductiveautomation.com/display/DOC80/Script+Transform](https://docs.inductiveautomation.com/display/DOC80/Script+Transform)

I have found this Doc but I don't know why It doesn't work for me. I want to display database values (time series) into apex chart so I had to bind the data with a transform to convert the query into an array

this is my code

```auto
	Data = system.db.runNamedQuery("level" )
	pyData = system.dataset.toPyDataSet(Data)
	
	newList = system.tag.read(pyData).value.getRowCount()
	for row in Data:
	    newDict = {}
	    for i in range(len(row)):
	        newDict[header[i] ] = row[i]
	     	newList.append(newDict)
	return newList
type or paste code here

```

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [September 3, 2023, 2:59pm UTC](https://forum.inductiveautomation.com/t/convert-named-query-into-an-array/78696/2 "2023-09-03T14:59:04Z")

</div>

There are multiple issue with that script, which no longer matches the linked manual page's examples. Most notably this line:

```auto
newList = system.tag.read(pyData).value.getRowCount()

```

1. pyData is not a tagPath, so `system.tag.read()` will not return a good value
2. newList is expected to be a `List` and so, even had this line returned something,  
it would have been an integer, and then the call to `newList.append()` would have failed.
3. Unless the list `header` is defined in a part of the script you haven't posted, then it is undifined and the script would fail there also.

What type of binding is this transform on? It seems odd to me to use a transform without using the value supplied to that transform.

To convert a PyDataset to a list of dictionaries you would need code something like this:

```auto
data = system.db.runNamedQuery("level")
pyData = system.dataset.toPyDataSet(data)

return [{c:row[idx] for idx,c in enumerate(pyData.columnNames)} for row in pyData]

```

Note that this will not maintain the the order of you're columns from row to row.

I am curious why a query binding with a return format set to JSON wont work here though. I don't have the apex charts module, so perhaps there is a difference that I am unaware of.

---

<div class="post-metadata">

**Author:** ![John\_Morgan1](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@John\_Morgan1](https://forum.inductiveautomation.com/u/John_Morgan1)\
**Post date:** [September 3, 2023, 7:37pm UTC](https://forum.inductiveautomation.com/t/convert-named-query-into-an-array/78696/3 "2023-09-03T19:37:14Z")

</div>

Sorry for my coding (I am not used to scripting yet) 😅

I am using the Apex chart module for the first time and from the docs it is accepting data with a certain format

> **[Series \[Working with Data\] – ApexCharts.js](https://apexcharts.com/docs/series/)**
>
> Know which formats are supported by ApexCharts to parse your data correctly whether it be timeline or categorized data for building interactive charts.

I am getting my data from the database (named query) and I want to convert the named query data to a data can be shown on the Apex chart

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [September 3, 2023, 7:46pm UTC](https://forum.inductiveautomation.com/t/convert-named-query-into-an-array/78696/4 "2023-09-03T19:46:03Z")

</div>

According to the documentation found here:

> **[GitHub - Kyvis-Labs/ignition-apexcharts-module: The module provides...](https://github.com/Kyvis-Labs/ignition-apexcharts-module)**
>
> The module provides Perspective ApexCharts components - GitHub - Kyvis-Labs/ignition-apexcharts-module: The module provides Perspective ApexCharts components

You can assign an Ignition Dataset directly to the series property and it should work, no conversion needed.
