Trying to extract tag data to make a dataset

'''data = system.tag.browse("[default]DataMemory")
headers = data.getResults()

keys_to_keep = ["name","value"]

filtered_list = [
{key: dct[key] for key in keys_to_keep}
for dct in headers
]

for dictionary in filtered_list:
for key, value_list in dictionary.iteritems():
dictionary[key] = [value_list[0]]

print(filtered_list)
'''
Hello All,

Currentky trying to extract tag data from a path to convert it to a dataset. For the value part of the tag though, it prints as the following:

[{'name': u'Flow Rate', 'value': [6, Good, Mon Aug 07 09:20:51 CDT 2023 (1691418051279)]}, {'name': u'Pressure', 'value': [2, Good, Mon Aug 07 09:20:59 CDT 2023 (1691418059829)]}, {'name': u'Temperature', 'value': [3, Good, Mon Aug 07 09:21:07 CDT 2023 (1691418067736)]}]

I would like to remove all the data from the value list except for the value itself. What I tried didn't work due to the BasicQualifiedValue object being unscriptable. Any ideas on how to do this differently that would work? Thank you so much in advance!

Instead of typing the single quotes, just use the </> button and paste your code inside the ticks provided.

You have a list of qualified values, to get to the value you have to extract it.

for dictionary in filtered_list:
    for key,value_list in dictionary.iteritems():
        dictionary[key] = [value_list[0].value]
# script builder. You may modify this script, but if you do,
# you will not be able to use the navigation builder to update
# this script without overwriting your changes.

data = system.tag.browse("[default]DataMemory")
headers = data.getResults()

keys_to_keep = ["name","value"]

filtered_list = [
	{key: dct[key] for key in keys_to_keep}
	for dct in headers
]

for dictionary in filtered_list:
	for key, value_list in dictionary.iteritems():
		dictionary[key] = [value_list[0].value]
		
print(filtered_list)

I got the following error: AttributeError: 'unicode' object has no attribute 'value'

Can you post the values of [default]DataMemory and data.getResults() so we can test your script in Script Console?

{u'fullPath': [default]DataMemory/Flow Rate, u'formatString': u'#,##0.##', u'hasChildren': False, u'dataType': Int4, u'name': u'Flow Rate', u'tagType': AtomicTag, u'attributes': [], u'valueSource': u'memory', u'value': [6, Good, Mon Aug 07 09:20:51 CDT 2023 (1691418051279)]}, {u'fullPath': [default]DataMemory/Pressure, u'formatString': u'#,##0.##', u'hasChildren': False, u'dataType': Int4, u'name': u'Pressure', u'tagType': AtomicTag, u'attributes': [], u'valueSource': u'memory', u'value': [2, Good, Mon Aug 07 09:20:59 CDT 2023 (1691418059829)]}, {u'fullPath': [default]DataMemory/Temperature, u'formatString': u'#,##0.##', u'hasChildren': False, u'dataType': Int4, u'name': u'Temperature', u'tagType': AtomicTag, u'attributes': [], u'valueSource': u'memory', u'value': [3, Good, Mon Aug 07 09:21:07 CDT 2023 (1691418067736)]}]

image

That's not valid JSON. How did you generate it? You should have right-clicked on DataMemory and selected Copy JSON from the context menu.

Issue resolved, needed syntax is filtered_data[0]['value'].value

I feel like you're making things way more complicated than they actually are.
I'd propose an alternative, but I can't even figure out what you're trying to do !
Can you give me an example of input and expected output ?

Lets say I have three tags: Pressure, Temperature, Flow Rate. Each of them have their own value. I want to collect those tags and their values, and then make the names of the tags the header for a dataset, and make the values the data for a dataset. I want to then save this dataset as a new dataset type tag. I just want the value itself from the tag though, not the tag quality and timestamp that come with it.

Apologies for the confusion

# script builder. You may modify this script, but if you do,
# you will not be able to use the navigation builder to update
# this script without overwriting your changes.

data = system.tag.browse("[default]DataMemory")
headers = data.getResults()
print (headers)

keys_to_keep = ["name","value"]

filtered_list = [
	{key: dct[key] for key in keys_to_keep}
	for dct in headers
]


new_data = []
for item in filtered_list:
	modified_item = {"name": item["name"], "value": item["value"].value}
	new_data.append(modified_item)
	
newlod = [{d['name']: d['value']} for d in new_data]	
headers, data = zip(*[d.items()[0] for d in newlod])
ds = system.dataset.toDataSet(headers, [data])

print(ds)
event.source.parent.getComponent('Power Table').data = ds


What are you doing with the dataset? What's the final objective?

Append the second dataset to the first dataset with the same column names.

And then do what? Display?

Yes

Realizing now though that when you send the dataset to a tag, its no longer a type '<type 'com.inductiveautomation.ignition.common.BasicDataset'>', its now a <type 'java.util.ArrayList'> so I am unsure what to do.

You already have the tags, why does this dataset need to be a tag as well?

Just build the dataset at point of use.

paths = ['path/to/tag1','path/to/tag2','path/to/tag3']

tagValues = [qv.v for qv in system.tag.readBlocking(paths)]
headers = [path.split('/')[-1:] for path in paths]
ds = system.dataset.toDataSet(headers,[tagValues])

Because I have them generating from two different buttons, so have to be able to store the first dataset in a tag. Also because I will need to convert each dataset back into a list of dicts at some point later.