# 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:** 14\
**Page:** 2

<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:35pm UTC](https://forum.inductiveautomation.com/t/yaml-payload-parsing/49103/21 "2021-07-22T20:35:09Z")

</div>

Yeah, I don’t think the YAML data he’s reading is well formed.

---

<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:40pm UTC](https://forum.inductiveautomation.com/t/yaml-payload-parsing/49103/22 "2021-07-22T20:40:29Z")

</div>

try using just a string, and not the tag value and see if it parses correctly

```auto
yaml = '''
data: 
  level: -5.937274933
  press: -23.74909973
  temp: 0.0009031557711
'''

```

---

<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:49pm UTC](https://forum.inductiveautomation.com/t/yaml-payload-parsing/49103/23 "2021-07-22T20:49:37Z")

</div>

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/f/9f08f5b9dbf2c8ca89717b8b55d8e8354da57b24.png)  
The code works when the yaml data is given as  
yaml = ‘’’  
data:  
level: -5.937274933  
press: -23.74909973  
temp: 0.0009031557711  
‘’’  
I am now certain that when the data is in the memory tag in ignition it does not maintain its format since it becomes a normal string  
Not sure how to proceed

---

<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:52pm UTC](https://forum.inductiveautomation.com/t/yaml-payload-parsing/49103/24 "2021-07-22T20:52:56Z")

</div>

Great! First maybe start off by using the hard-coded yaml string and comparing it to the tag value.

```auto
print repr(yamlString)
print repr(tagYamlString)

```

---

<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, 9:01pm UTC](https://forum.inductiveautomation.com/t/yaml-payload-parsing/49103/25 "2021-07-22T21:01:46Z")

</div>

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/6/16c33965e89cebe372e5001b12ec5eca5627546c.png)  
Seems like there is a space difference in the tag version of the data vs manually entered yaml data

---

<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, 9:01pm UTC](https://forum.inductiveautomation.com/t/yaml-payload-parsing/49103/26 "2021-07-22T21:01:58Z")

</div>

If you’re populating the memory tag value with the tag editor in the Designer then you’ll lose your formatting, but if you wrote the YAML data to it via scripting or when it comes from a “real” source the format will be preserved.

---

<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, 9:07pm UTC](https://forum.inductiveautomation.com/t/yaml-payload-parsing/49103/27 "2021-07-22T21:07:18Z")

</div>

For this application I am receiving the payload over a MQTT stream. The MQTT stream is supposed to send YAML format. It comes in into a memory tag.  
Currently before I test the live data stream I have been trying to do the same by testing it with a data set which I enter manually using the tag editor into a memory tag.

Also to add to the conversation above,

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

Manually entered yaml data is stored with “u” infront of the data which is probably the unicode I am guessing

---

<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, 9:15pm UTC](https://forum.inductiveautomation.com/t/yaml-payload-parsing/49103/28 "2021-07-22T21:15:48Z")

</div>

The data value for your memory tag seems to be changing in between your screenshots as this post has progressed. As it is in the last one, with the newline characters, I would expect it to parse.

---

<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 23, 2021, 11:51pm UTC](https://forum.inductiveautomation.com/t/yaml-payload-parsing/49103/29 "2021-07-23T23:51:21Z")

</div>

The website prevented me from commenting after my 20 comment quota for the day.

You were right about your statement above and it did parse.  
I have tested it with the live stream of data and it works.  
Thank you @Kevin.Herron @dkhayes117 @paul-griffith  
One of my colleagues pointed the following link and may be another path for this issue as well.

> **[Ignition Exchange](https://www.inductiveautomation.com/exchange/2085/overview)**
>
> Discover, share, and download community made Ignition resources in collaboration with your organization or the Ignition community.

---

<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 24, 2021, 12:22am UTC](https://forum.inductiveautomation.com/t/yaml-payload-parsing/49103/30 "2021-07-24T00:22:07Z")

</div>

Glad you got it working! You never know, I may need to do this in the future 🙂

---

<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:** [December 20, 2022, 1:30am UTC](https://forum.inductiveautomation.com/t/yaml-payload-parsing/49103/31 "2022-12-20T01:30:41Z")

</div>

When I use the Ignition Exhange resource mentioned above I get this error only when running as a tag change script or gateway tag change event script while trying to convery python dictionary to yaml.

> com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last): File "", line 1, in ImportError: Error loading module core: Traceback (most recent call last): File "", line 59, in ImportError: Error loading module dumper: Traceback (most recent call last): File "", line 18, in ImportError: Error loading module representer: Traceback (most recent call last): File "", line 298, in File "", line 91, in add\_representer TypeError: unhashable type: 'stringmap'
> 
> at org.python.core.Py.ImportError(Py.java:329)
> 
> at com.inductiveautomation.ignition.common.script.ScriptModule.loadModule(ScriptModule.java:79)
> 
> at com.inductiveautomation.ignition.common.script.ScriptPackage. **findattr\_ex** (ScriptPackage.java:84)
> 
> at org.python.core.PyObject. **findattr** (PyObject.java:902)
> 
> at org.python.core.PyObject.impAttr(PyObject.java:1035)
> 
> at org.python.core.imp.import\_next(imp.java:1161)
> 
> at org.python.core.imp.import\_logic(imp.java:1278)
> 
> at org.python.core.imp.import\_module\_level(imp.java:1369)
> 
> at org.python.core.imp.importName(imp.java:1528)
> 
> at org.python.core.ImportFunction. **call** ( **builtin**.java:1285)
> 
> at org.python.core.PyObject. **call** (PyObject.java:433)
> 
> at org.python.core. **builtin**. **import** ( **builtin**.java:1232)
> 
> at org.python.core.imp.importFromAs(imp.java:1620)
> 
> at org.python.core.imp.importFrom(imp.java:1595)
> 
> at org.python.pycode.\_pyx813.f$0(:3)
> 
> at org.python.pycode.\_pyx813.call\_function()
> 
> at org.python.core.PyTableCode.call(PyTableCode.java:173)
> 
> at org.python.core.PyCode.call(PyCode.java:18)
> 
> at org.python.core.Py.runCode(Py.java:1687)
> 
> at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:803)
> 
> at com.inductiveautomation.ignition.gateway.project.ProjectScriptLifecycle$TrackingProjectScriptManager.runCode(ProjectScriptLifecycle.java:819)
> 
> at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:751)
> 
> at com.inductiveautomation.ignition.gateway.project.ProjectScriptLifecycle$TrackingProjectScriptManager.runCode(ProjectScriptLifecycle.java:800)
> 
> at com.inductiveautomation.ignition.common.script.TagChangeScriptExecutor$TagChangeExecutionCallback.execute(TagChangeScriptExecutor.java:242)
> 
> at com.inductiveautomation.ignition.common.script.TagChangeScriptExecutor$TagChangeExecutionCallback.execute(TagChangeScriptExecutor.java:194)
> 
> at com.inductiveautomation.ignition.common.util.SerialExecutionQueue$PollAndExecute.run(SerialExecutionQueue.java:102)
> 
> at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
> 
> at java.base/java.util.concurrent.FutureTask.run(Unknown Source)
> 
> at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
> 
> at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
> 
> at java.base/java.lang.Thread.run(Unknown Source)
> 
> Caused by: org.python.core.PyException: ImportError: Error loading module core: Traceback (most recent call last): File "", line 59, in ImportError: Error loading module dumper: Traceback (most recent call last): File "", line 18, in ImportError: Error loading module representer: Traceback (most recent call last): File "", line 298, in File "", line 91, in add\_representer TypeError: unhashable type: 'stringmap'

---

<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:** [December 20, 2022, 1:43am UTC](https://forum.inductiveautomation.com/t/yaml-payload-parsing/49103/32 "2022-12-20T01:43:10Z")

</div>

> from shared.tools.yaml import core

Executing this line in the script console executes and imports the function right.  
When I do the same in the gateway tag change script, I encounter the above.  
Any help would be great !

---

<div class="post-metadata">

**Author:** ![andrew.geiger](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/andrew.geiger/32/5219_2.png) [@andrew.geiger](https://forum.inductiveautomation.com/u/andrew.geiger)\
**Post date:** [December 20, 2022, 4:09am UTC](https://forum.inductiveautomation.com/t/yaml-payload-parsing/49103/33 "2022-12-20T04:09:40Z")

</div>

Sorry to hear that - I've updated the module to correct for the import bug. While it's in review, you can download it here.

[Ignition-8-shared.tools.yaml-v1.1.0.zip](https://forum.inductiveautomation.com/uploads/short-url/tcVevg08V46P9T6ebDK8Pe7iDmc.zip) (52.5 KB)

What you saw was a bit of an inside baseball look into how the gateway scope is a little different from the designer. The `from module import *` directive was pulling in a lot of unexpected stuff that managed to confuse the representer loading. This isn't a bug in how the import works, just a missed ` __all__ ` entry from me being sloppy.

I tested it in a tag script, also saw your error, and applied the above to patch it. Cheers!

---

<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:** [December 21, 2022, 6:28pm UTC](https://forum.inductiveautomation.com/t/yaml-payload-parsing/49103/34 "2022-12-21T18:28:16Z")

</div>

@andrew.geiger Thank you for the modified code above. I have tested it on my app and it works well. Appriciate your quick response.

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