YAML payload parsing

Has anyone tried to decode YAML payload in Ignition using scripting ?
The application I am working on has a YAML payload which needs to be parsed to obtain specific values on the tags.
I tried using pyyaml library and it has been not working for me due to its C dependency.
Is there a work around for this issue ?
Example payload:

data: 
  level: -5.937274933
  press: -23.74909973
  temp: 0.0009031557711

The 'right' way to do it would be to create a custom module that brought a Java YAML parser into scope - it looks like the two main candidates are Jackson or 'snakeyaml'.
The 'easy' way to do it would be to use the 'hack' described here:

If you drop the snakeyaml .jar file in the right location, the gateway will automatically load it to the classpath on the next restart, which will then make it available from scripting.

1 Like

I also saw com.esotericsoftware.yamlbeans too.
YamlReader (YamlBeans 1.15 API) (javadoc.io)

I took a stab at it with my limited Java knowledge, I have no idea if this will work but is a starting point

from com.esotericsoftware.yamlbeans import YamlReader as YR
from java.io import FileInputStream
filePath = 'L:\yamlFile.yaml'.toPath()
yaml = FileInputStream(filePath.read())

yamlReader = YR(yaml)
result = yamlReader.read()
print result
1 Like

image
I downloaded the yamlbeans-1.13.jar file and placed it in
C:\Program Files\Inductive Automation\Ignition\lib\core\gateway and restarted the gateway

I am not a java programmer. Would appreciate any help !

You’d have to place it in lib/core/common to be able to access it from the script console in the Designer like that.

(And restart both Gateway and Designer)

1 Like

Placing the .jar file in the lib/core/common folder worked.
Program executed:
from com.esotericsoftware.yamlbeans import YamlReader as YR
yamlString = system.tag.read("[default]mqtttemplate.value")
yamlReader = YR(str(yamlString))
result =YR.read

I am still working on how to retrieve the data values from each object. When I read the result it just gives a return type.
image

What should I change to retrieve individual values ?

Did you try YR.read()? Looks like YR.Read is giving you the function reference back.

1 Like

Also should it not be

yamlString = system.tag.read("[default]mqtttemplate").value

Just noticed that the memory tag I am reading in the system.tag.read is having the quality data in it. May be this is causing the issue ?

The “.value” should not be part of your tag path. The result of a tag read is a QualifiedValue object and you need to then pull the value out of it. See how @dkhayes117 has it on the end, outside the parens.

1 Like

@Kevin.Herron @dkhayes117
Made the “.value” change. That fixed QualifiedValue object.

How to retrieve a value out of this data ?
data: ~
level: -5.937274933
press: -23.74909973
temp: 0.0009031557711
For example ---- > How to get level value ?

Again, you aren’t actually calling the read method because you didn’t parenthesis at the end.

3 Likes

As @Kevin.Herron said…result = yamlReader.read()


I was able to correct the syntax issues.
Suggestions on how to retrieve level data?
Is yaml format maintained when stored in a Memory tag ?
result only was able to get the first level of the yaml data
Thoughts ??

What’s the result of:

print result['data']
print result['data']['level']

image

try

print type(result)
1 Like

<type 'unicode'>

Well I guess it returned a string and not a hashmap or dictionary, and since it printed u'data' when you printed it, that tells me it didn’t parse the yaml except for the first line. I suspect that the yaml isn’t formatted correctly. Maybe you should take your “yaml” and paste into an online formatting validator to make sure it follows yaml structure.

EDIT I checked it and it was a valid yaml…

Try using this for the text

--- 
data: 
  level: -5.937274933
  press: -23.74909973
  temp: 0.0009031557711