Ignition 8 : error writing dictionnary to a Document Tag

Ignition 8.0.13
When I try to write a gateway Document Tag with a dictionnary, I have the following error:

dico = {"test" : "10"}
system.tag.writeBlocking(["[default]path/to/tag"],[dico])
[Error_TypeConversion("Cannot coerce to document from class type 'class org.python.core.PyDictionary'")]

What’s wrong ?

1 Like

I don’t know why but there’s no auto-conversion from a Python Dictionary to JSON when writing to Document tags.

Encode your dictionary with system.util.jsonEncode before writing it.

4 Likes

To clarify, you also need to convert it into a dictioanary when reading it as well if you want to manipulate it.
i.e.

documentTag = system.tag.readBlocking([tagPath])[0].value
document = system.util.jsonDecode(str(documentTag))

If you dont do this then the DocumentObject wrapping the top level keeps you from being able to write to anything inside the document

So to edit the document you have to go through the following steps:

  1. Read the tag
  2. Cast it as a string
  3. jsonDecode it into a dictionary
  4. manipulate the dictionary
  5. jsonEncode it back into a string
  6. Write your changes back to the tag

Feels a bit overcomplicated, but I would just assume that the auto-conversion would probably be a good feature request, as with the rise of perspective, document tags are going to start becoming a lot more popular.

1 Like

You can skip steps 2 and 3 in the response from kgamble above. The read document value has a .toDict() function to jump to a dictionary, then modify, encode and write back.

3 Likes