Using Document Tags to Store JSON

Hi I’m trying to utilize document tags to store some data, and having a hard time with it.

Heres what I’m doing:
Create an empty document tag.
in scripting, create a dictionary, that will be a list of dictionaries.
write that structure to the tag
then I want to append additional entries to the list, which I get an error.

#get the tag value
tagData = system.tag.readBlocking([myDocTag])[0].value
#create an instance of data i want to store
instance = {'stuff':'stuff info'}
#if the tag is empty, create the data structure
if tagData is None:
	data = {'toplevel' : [instance]}
#the structure should exist, so just add to it
	tagData = dict(tagData)
	tagData['toplevel'].append(instance) #I get an error here  
#can't convert to com.inductiveautomation.ignition.common.document.DocumentElement

data = system.util.jsonEncode(tagData)
system.tag.writeBlocking([myDocTag], [data])

Is there something I’m missing?

Well I think your first mistake is assuming the value you get from reading a document tag is a python dictionary.

You can convert it to a dictionary with something like this:

qv ="DocTest")
dict = system.util.jsonDecode(str(qv.value))

The next issue is that dictionaries don’t have an append method…

1 Like

You can also use .toDict():

qv ="DocTest")
doctest_dict = qv.value.toDict()

Also, just a quick warning. If you store java.util.Date type values to these tags, they get converted to a string and you will need to loop through the dict to parse them back to a Date object if you want to use them as a Date object again (, "E MMM dd HH:mm:ss z yyyy")).

1 Like

Hrm, I do convert the value i get from a tag using
(after the else:)
tagData = dict(tagData)

I’ll try to use json decode like your example though.

I’m not trying to append to dict, i’m trying to append to a list thats the value of a key in dictionary. like this:

test = {'top': []}
print test
>>> {'top': [1, 2, 3]}

well after getting the tag value, I didnt realize I needed to force it to a string first. Thanks that helped!

@WillMT10’s toDict() method is a little cleaner than forcing it to string and parsing it.

1 Like

so is
different than
dict(system.tag.readBlocking([myDocTag])[0].value) ?

Yes, because toDict() is something we implemented on the Python-friendly document type you get from reading that tag’s value. I don’t know how dict() will behave when receiving that object.

oh ok that makes sense. sweet well i’ll try that, thanks!

Is this something recent, or did I completely miss it somewhere? Up til now I was importing and using the functions in TypeUtilities.

I didn’t know it was there either… but it looks like it has been since 2018.