# \[Perspective\] Named Query Return Format

**URL:** <https://forum.inductiveautomation.com/t/perspective-named-query-return-format/54193>\
**Category:** Ignition\
**Tags:** ignition81, scripting, perspective\
**Created:** [December 16, 2021, 1:07pm UTC](https://forum.inductiveautomation.com/t/perspective-named-query-return-format/54193 "2021-12-16T13:07:54Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Davide3i](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/davide3i/32/27811_2.png) [@Davide3i](https://forum.inductiveautomation.com/u/Davide3i)\
**Post date:** [December 16, 2021, 1:07pm UTC](https://forum.inductiveautomation.com/t/perspective-named-query-return-format/54193/1 "2021-12-16T13:07:54Z")

</div>

Hello.  
I’d like to know if is there a way to change the **Return Format** of a **Named Query** called via a Gateway script, similarly to what is possible to achieve by using a **Query Binding** as shown in the attached image.

It doesn’t look possible from what I can read from the [official doc](https://docs.inductiveautomation.com/display/DOC81/system.db.runNamedQuery), but let me know if I’m missing out something.

I’m having issues uploading the image: [here](https://imgur.com/a/YMb9nHo) an external link.

---

<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:** [December 16, 2021, 1:15pm UTC](https://forum.inductiveautomation.com/t/perspective-named-query-return-format/54193/2 "2021-12-16T13:15:52Z")

</div>

doesnt look like it, why would you need that?

---

<div class="post-metadata">

**Author:** ![Davide3i](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/davide3i/32/27811_2.png) [@Davide3i](https://forum.inductiveautomation.com/u/Davide3i)\
**Post date:** [December 16, 2021, 1:19pm UTC](https://forum.inductiveautomation.com/t/perspective-named-query-return-format/54193/3 "2021-12-16T13:19:09Z")

</div>

I would like to receive the dataset in JSON format as in the attached image, without converting it using a script. Unfortunately I must use a legacy function which takes as an input a dictionary and I don’t have time to refactor it to accept a dataset or a pydataset.

---

<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:** [December 16, 2021, 1:28pm UTC](https://forum.inductiveautomation.com/t/perspective-named-query-return-format/54193/4 "2021-12-16T13:28:44Z")

</div>

Just use the TypeUtilities and convert the dataset to JSON and then call your function.

```auto
from com.inductiveautomation.ignition.common import TypeUtilities
jsonStr = str(TypeUtilities.datasetToJSON(<your dataset>))

```

---

<div class="post-metadata">

**Author:** ![Davide3i](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/davide3i/32/27811_2.png) [@Davide3i](https://forum.inductiveautomation.com/u/Davide3i)\
**Post date:** [December 16, 2021, 1:39pm UTC](https://forum.inductiveautomation.com/t/perspective-named-query-return-format/54193/5 "2021-12-16T13:39:54Z")

</div>

Hello, thanks for your message.  
It doesn’t look like I’m obtaining a JSON looking like the one I’m obtaining through **Return Format**.  
It simply return the whole dataset structure as a JSON.

---

<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:** [December 16, 2021, 1:41pm UTC](https://forum.inductiveautomation.com/t/perspective-named-query-return-format/54193/6 "2021-12-16T13:41:35Z")

</div>

You dont actually want a json, but you want a python dict. idk why igntion called it json format when its not rly a json…

---

<div class="post-metadata">

**Author:** ![Davide3i](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/davide3i/32/27811_2.png) [@Davide3i](https://forum.inductiveautomation.com/u/Davide3i)\
**Post date:** [December 16, 2021, 1:47pm UTC](https://forum.inductiveautomation.com/t/perspective-named-query-return-format/54193/7 "2021-12-16T13:47:06Z")

</div>

Yep, it looks like a dictionary, after a closer look. I’ll try to find a way to convert a dataset to a dictionary in a smart way.

---

<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:** [December 16, 2021, 1:57pm UTC](https://forum.inductiveautomation.com/t/perspective-named-query-return-format/54193/8 "2021-12-16T13:57:18Z")

</div>

```auto
def datasetToDictArry(value):
	pyDictArry = []
	for row in range(value.getRowCount()):
		pyDictRow = {}
		for col in value.getColumnNames():
			pyDictRow[col] = value.getValueAt(row, col)
		pyDictArry.append(pyDictRow)
	return pyDictArry

ds=aDataset

datasetToDictArry(ds)

```

you can put this in a project script so you can use it everywhere you want

---

<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:** [December 16, 2021, 2:03pm UTC](https://forum.inductiveautomation.com/t/perspective-named-query-return-format/54193/9 "2021-12-16T14:03:35Z")

</div>

What Key:Value format is your function expecting and how does that relate to the dataset returned from query?

---

<div class="post-metadata">

**Author:** ![Davide3i](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/davide3i/32/27811_2.png) [@Davide3i](https://forum.inductiveautomation.com/u/Davide3i)\
**Post date:** [December 16, 2021, 2:08pm UTC](https://forum.inductiveautomation.com/t/perspective-named-query-return-format/54193/10 "2021-12-16T14:08:03Z")

</div>

> [@victordcq](#):
>
> ```auto
> def datasetToDictArry(value):
> pyDictArry = []
> for row in range(value.getRowCount()):
> pyDictRow = {}
> for col in value.getColumnNames():
> pyDictRow[col] = value.getValueAt(row, col)
> pyDictArry.append(pyDictRow)
> return pyDictArry
> 
> ```

Yup, I used something like that.

```auto
def dataset2dic(dataset):
	cols = dataset.getColumnNames()
	
	return [{col: dataset.getValueAt(i, col) for col in cols} for i in range(dataset.rowCount)]

```

Thank you both for your help!
