# OPC writing errors with jsonSet() in nested folders

**URL:** <https://forum.inductiveautomation.com/t/opc-writing-errors-with-jsonset-in-nested-folders/58745>\
**Category:** General Discussion\
**Created:** [April 22, 2022, 2:46pm UTC](https://forum.inductiveautomation.com/t/opc-writing-errors-with-jsonset-in-nested-folders/58745 "2022-04-22T14:46:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jbartsch](https://avatars.discourse-cdn.com/v4/letter/j/a183cd/32.png) [@jbartsch](https://forum.inductiveautomation.com/u/jbartsch)\
**Post date:** [April 22, 2022, 2:46pm UTC](https://forum.inductiveautomation.com/t/opc-writing-errors-with-jsonset-in-nested-folders/58745/1 "2022-04-22T14:46:26Z")

</div>

I am hoping someone can point me in the right direction, or offer up some suggestions. I are attempting to write to a Siemens PLC over OPC using nested derived tags in UDT folder.  
Root derived tags are good with read and writes but the nested tags will read fine but when written to, I get… Bad(“Bad\_EncodingError: Encoding halted because of invalid data in the objects being serialized.”)

I feel like I’m missing something very basic, here’s the setup:

UDT Structure  
\_sourceOPC = JSON value for name/value and objects  
Value Source = OPC  
Data Type = Document  
OPC Server = myOPC  
OPC Item Path = myBrowsedPath  
Derived\_Tag1  
Source Tag Path = \_sourceOPC  
Read Expression = jsonGet({source},“Tag1”)  
Write Expression = jsonSet({source},“Tag1”,{value})  
Folder  
\_sourceDerived = JSON value for Object1  
Source Tag Path = \_sourceOPC  
Read Expression = jsonGet({source},“Object1”)  
Write Expression = jsonSet({source},“Object1”,{value})  
Derived\_Tag2  
Source Tag Path = \_sourceDerived  
Read Expression = jsonGet({source},“Tag2”)  
Write Expression = jsonSet({source},“Tag2”,{value})

Error only occurs on Tag2 writes, Tag2 reads are good

---

<div class="post-metadata">

**Author:** ![CosmoStevens](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cosmostevens/32/32309_2.png) [@CosmoStevens](https://forum.inductiveautomation.com/u/CosmoStevens)\
**Post date:** [April 27, 2022, 10:50pm UTC](https://forum.inductiveautomation.com/t/opc-writing-errors-with-jsonset-in-nested-folders/58745/2 "2022-04-27T22:50:07Z")

</div>

Hello,

Do tag1 and tag2 have the same configurations? Are you able to write to tag2’s source tag path directly without involving the derived tag? If so, is it possible that there is a datatype mismatch involved?

---

<div class="post-metadata">

**Author:** ![jbartsch](https://avatars.discourse-cdn.com/v4/letter/j/a183cd/32.png) [@jbartsch](https://forum.inductiveautomation.com/u/jbartsch)\
**Post date:** [April 28, 2022, 3:18pm UTC](https://forum.inductiveautomation.com/t/opc-writing-errors-with-jsonset-in-nested-folders/58745/3 "2022-04-28T15:18:41Z")

</div>

I feel like it’s a syntax issue in the jsonSet() that is not accessing the object correctly in the parent JSON. In the image, the green writes works but the red does not. To test I created an “\_OPC Object1” by browsing the Siemens OPC item path, drilling down in DataBlocksGlobal folder to structure. I confirmed that both the “\_derived Object1 from sourceOPC JSON parent” and “\_OPC Object1” have the same JSON.

_ **\_sourceOPC JSON parent** _ value looks like:

- [{  
“PLC Scan Value 1 PN\_Lot”: “”,  
“PLC Scan Value 2 PN\_Lot”: “”,  
“PLC\_EDC Stock Counters”: { “Anchor Bolts”: 26981, “nextName”:0}},Good, Thu Apr 28 10:19:43 EDT 2022 (1651155583066)]

_ **\_derived Object1 from sourceOPC JSON parent** _ :

- Write Expression = jsonSet({source},“PLC\_EDC Stock Counters”,{value})

_ **derived Name1 from derived Object1** _

- Write Expression = jsonSet({source},“Anchor Bolts”,{value})

Are my write expressions correct?

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/d/bd29b677072b3f99c195722b7a01a24f4af3d30b.png)

---

<div class="post-metadata">

**Author:** ![CosmoStevens](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cosmostevens/32/32309_2.png) [@CosmoStevens](https://forum.inductiveautomation.com/u/CosmoStevens)\
**Post date:** [April 28, 2022, 9:28pm UTC](https://forum.inductiveautomation.com/t/opc-writing-errors-with-jsonset-in-nested-folders/58745/4 "2022-04-28T21:28:15Z")

</div>

The expressions look fine to me, but troubleshooting the problem as a whole may require a closer look. I would suggest [contacting support](https://inductiveautomation.com/support/home/) to get a more thorough investigation.

---

<div class="post-metadata">

**Author:** ![jbartsch](https://avatars.discourse-cdn.com/v4/letter/j/a183cd/32.png) [@jbartsch](https://forum.inductiveautomation.com/u/jbartsch)\
**Post date:** [April 29, 2022, 11:51am UTC](https://forum.inductiveautomation.com/t/opc-writing-errors-with-jsonset-in-nested-folders/58745/5 "2022-04-29T11:51:08Z")

</div>

will do, thank you for your feedback

---

<div class="post-metadata">

**Author:** ![MMAspelien](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@MMAspelien](https://forum.inductiveautomation.com/u/MMAspelien)\
**Post date:** [September 22, 2022, 7:56am UTC](https://forum.inductiveautomation.com/t/opc-writing-errors-with-jsonset-in-nested-folders/58745/6 "2022-09-22T07:56:31Z")

</div>

I’m having the same problem with writing to nested derived tags. Did you ever get a solution on this from support?

---

<div class="post-metadata">

**Author:** ![jbartsch](https://avatars.discourse-cdn.com/v4/letter/j/a183cd/32.png) [@jbartsch](https://forum.inductiveautomation.com/u/jbartsch)\
**Post date:** [September 22, 2022, 12:17pm UTC](https://forum.inductiveautomation.com/t/opc-writing-errors-with-jsonset-in-nested-folders/58745/7 "2022-09-22T12:17:40Z")

</div>

I am still a bit fuzzy on this, we are reading/writing to Siemens PLC and apparently there is some issues with how the white spaces in the key:value are handled. Reading doesn’t have give me issues but when writing we get: “‘Bad(“Bad\_EncodingError: Encoding halted because of invalid data in the objects being serialized.”)’”

I am having positive results using the _ **\_sourceOPC JSON parent** _ as the source and then using following:  
jsonGet({source},“PLC\_EDC Stock Counters[‘Anchor Bolts’]”)  
jsonSet({source},“PLC\_EDC Stock Counters[‘Anchor Bolts’]”,{value})

jsonSet({source},“PLC\_EDC Stock Counters.Anchor Bolts”,{value}) gives me encoding error.

Hope this helps.

---

<div class="post-metadata">

**Author:** ![MMAspelien](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@MMAspelien](https://forum.inductiveautomation.com/u/MMAspelien)\
**Post date:** [September 26, 2022, 7:11am UTC](https://forum.inductiveautomation.com/t/opc-writing-errors-with-jsonset-in-nested-folders/58745/8 "2022-09-26T07:11:35Z")

</div>

Thank you.

We are also using Siemens PLC and get the “Bad\_EncodingError”. When I look at the logs I can see that on writing the derived object doesn’t parse correctly (newlines are parsed as “\n” and isn’t correct json format).

I tried your solution:  
jsonSet({source},“PLC\_EDC Stock Counters[‘Anchor Bolts’]”,{value})  
and the Bad\_Encoding error disappeared, but instead I got “Bad(“Bad\_NotWritable: The access level does not allow writing to the Node.”)”… At least I’m one step closer to something.

---

<div class="post-metadata">

**Author:** ![MMAspelien](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@MMAspelien](https://forum.inductiveautomation.com/u/MMAspelien)\
**Post date:** [September 26, 2022, 7:26am UTC](https://forum.inductiveautomation.com/t/opc-writing-errors-with-jsonset-in-nested-folders/58745/9 "2022-09-26T07:26:31Z")

</div>

Actually it was working, the NotWritable error was just because not all of the tags in PLC was set to writable.
