[Perspective] Named Query Return Format

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, but let me know if I’m missing out something.

I’m having issues uploading the image: here an external link.

doesnt look like it, why would you need that?

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.

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

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

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.

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…

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.

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

1 Like

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

2 Likes

Yup, I used something like that.

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!

1 Like