# YAML payload parsing

**URL:** <https://forum.inductiveautomation.com/t/yaml-payload-parsing/49103>\
**Category:** General Discussion\
**Tags:** ignition80, scripting\
**Created:** [July 22, 2021, 5:19pm UTC](https://forum.inductiveautomation.com/t/yaml-payload-parsing/49103 "2021-07-22T17:19:33Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![besaliealprince](https://avatars.discourse-cdn.com/v4/letter/b/0ea827/32.png) [@besaliealprince](https://forum.inductiveautomation.com/u/besaliealprince)\
**Post date:** [July 22, 2021, 5:19pm UTC](https://forum.inductiveautomation.com/t/yaml-payload-parsing/49103/1 "2021-07-22T17:19:33Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [July 22, 2021, 5:36pm UTC](https://forum.inductiveautomation.com/t/yaml-payload-parsing/49103/2 "2021-07-22T17:36:31Z")

</div>

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:

> [@Importing and using 3rd party Java jar libraries in Ignition](https://forum.inductiveautomation.com/t/importing-and-using-3rd-party-java-jar-libraries-in-ignition/11342/3):
>
> For gateway-only access you can also just drop jar files into the lib/core/gateway folder (and then restart). Just don’t expect these to be part of any backups, available outside gateway scope, or available on another Ignition gateway unless you put them there also. Basically, you need to remember that you added those jars, else your project may someday stop working because you’ve found yourself running on an installation that does not have them.

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.

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [July 22, 2021, 5:41pm UTC](https://forum.inductiveautomation.com/t/yaml-payload-parsing/49103/3 "2021-07-22T17:41:25Z")

</div>

I also saw `com.esotericsoftware.yamlbeans` too.  
[YamlReader (YamlBeans 1.15 API) (javadoc.io)](https://javadoc.io/static/com.esotericsoftware.yamlbeans/yamlbeans/1.15/com/esotericsoftware/yamlbeans/YamlReader.html)

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

```auto
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

```

---

<div class="post-metadata">

**Author:** ![besaliealprince](https://avatars.discourse-cdn.com/v4/letter/b/0ea827/32.png) [@besaliealprince](https://forum.inductiveautomation.com/u/besaliealprince)\
**Post date:** [July 22, 2021, 6:05pm UTC](https://forum.inductiveautomation.com/t/yaml-payload-parsing/49103/4 "2021-07-22T18:05:14Z")

</div>

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/1/b15610264756c954a8bd8068943b40f9ef6b822c.png)  
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 !

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [July 22, 2021, 6:07pm UTC](https://forum.inductiveautomation.com/t/yaml-payload-parsing/49103/5 "2021-07-22T18:07:49Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![besaliealprince](https://avatars.discourse-cdn.com/v4/letter/b/0ea827/32.png) [@besaliealprince](https://forum.inductiveautomation.com/u/besaliealprince)\
**Post date:** [July 22, 2021, 7:06pm UTC](https://forum.inductiveautomation.com/t/yaml-payload-parsing/49103/6 "2021-07-22T19:06:15Z")

</div>

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](https://us1.discourse-cdn.com/inductiveautomation/original/3X/a/c/ac8b45e88d5fed4af622a2efb322e85fe0e5d78f.png)

What should I change to retrieve individual values ?

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [July 22, 2021, 7:12pm UTC](https://forum.inductiveautomation.com/t/yaml-payload-parsing/49103/7 "2021-07-22T19:12:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [July 22, 2021, 7:18pm UTC](https://forum.inductiveautomation.com/t/yaml-payload-parsing/49103/8 "2021-07-22T19:18:36Z")

</div>

Also should it not be

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

```

---

<div class="post-metadata">

**Author:** ![besaliealprince](https://avatars.discourse-cdn.com/v4/letter/b/0ea827/32.png) [@besaliealprince](https://forum.inductiveautomation.com/u/besaliealprince)\
**Post date:** [July 22, 2021, 7:29pm UTC](https://forum.inductiveautomation.com/t/yaml-payload-parsing/49103/9 "2021-07-22T19:29:01Z")

</div>

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 ?

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/a/9a1b335ee0713fccf41415bac0ef6c4c931583b1.png)

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [July 22, 2021, 7:33pm UTC](https://forum.inductiveautomation.com/t/yaml-payload-parsing/49103/10 "2021-07-22T19:33:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![besaliealprince](https://avatars.discourse-cdn.com/v4/letter/b/0ea827/32.png) [@besaliealprince](https://forum.inductiveautomation.com/u/besaliealprince)\
**Post date:** [July 22, 2021, 7:45pm UTC](https://forum.inductiveautomation.com/t/yaml-payload-parsing/49103/11 "2021-07-22T19:45:17Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/f/4/f4af51529a31387873f26b993439552d4cb36cc3.png)

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 ?

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [July 22, 2021, 7:47pm UTC](https://forum.inductiveautomation.com/t/yaml-payload-parsing/49103/12 "2021-07-22T19:47:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [July 22, 2021, 8:02pm UTC](https://forum.inductiveautomation.com/t/yaml-payload-parsing/49103/13 "2021-07-22T20:02:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![besaliealprince](https://avatars.discourse-cdn.com/v4/letter/b/0ea827/32.png) [@besaliealprince](https://forum.inductiveautomation.com/u/besaliealprince)\
**Post date:** [July 22, 2021, 8:18pm UTC](https://forum.inductiveautomation.com/t/yaml-payload-parsing/49103/14 "2021-07-22T20:18:38Z")

</div>

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/0/6065ed911eca9407b9f3873f1c92909be2bac9ad.png)  
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 ??

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [July 22, 2021, 8:24pm UTC](https://forum.inductiveautomation.com/t/yaml-payload-parsing/49103/15 "2021-07-22T20:24:43Z")

</div>

What’s the result of:

```auto
print result['data']
print result['data']['level']

```

---

<div class="post-metadata">

**Author:** ![besaliealprince](https://avatars.discourse-cdn.com/v4/letter/b/0ea827/32.png) [@besaliealprince](https://forum.inductiveautomation.com/u/besaliealprince)\
**Post date:** [July 22, 2021, 8:26pm UTC](https://forum.inductiveautomation.com/t/yaml-payload-parsing/49103/16 "2021-07-22T20:26:47Z")

</div>

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/9/89ba919d7440c6be92ef0e760d2b0bffc2a5bc23.png)

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [July 22, 2021, 8:27pm UTC](https://forum.inductiveautomation.com/t/yaml-payload-parsing/49103/17 "2021-07-22T20:27:13Z")

</div>

try

```auto
print type(result)

```

---

<div class="post-metadata">

**Author:** ![besaliealprince](https://avatars.discourse-cdn.com/v4/letter/b/0ea827/32.png) [@besaliealprince](https://forum.inductiveautomation.com/u/besaliealprince)\
**Post date:** [July 22, 2021, 8:27pm UTC](https://forum.inductiveautomation.com/t/yaml-payload-parsing/49103/18 "2021-07-22T20:27:34Z")

</div>

> [@dkhayes117](#):
>
> `print type(result)`

\<type 'unicode'\>

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [July 22, 2021, 8:30pm UTC](https://forum.inductiveautomation.com/t/yaml-payload-parsing/49103/19 "2021-07-22T20:30:12Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [July 22, 2021, 8:34pm UTC](https://forum.inductiveautomation.com/t/yaml-payload-parsing/49103/20 "2021-07-22T20:34:04Z")

</div>

Try using this for the text

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

```

[Next page](https://forum.inductiveautomation.com/t/yaml-payload-parsing/49103.md?page=2)
