# \[IGN-5231\] jsonEncode issue

**URL:** <https://forum.inductiveautomation.com/t/ign-5231-jsonencode-issue/44603>\
**Category:** Ignition\
**Created:** [March 17, 2021, 10:04pm UTC](https://forum.inductiveautomation.com/t/ign-5231-jsonencode-issue/44603 "2021-03-17T22:04:34Z")\
**Posts on this page:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![dandersen](https://avatars.discourse-cdn.com/v4/letter/d/ecb155/32.png) [@dandersen](https://forum.inductiveautomation.com/u/dandersen)\
**Post date:** [March 18, 2021, 6:50pm UTC](https://forum.inductiveautomation.com/t/ign-5231-jsonencode-issue/44603/22 "2021-03-18T18:50:49Z")

</div>

@WillMT10 I think you are correct about the `dictproxy` part. I put a typecast to force it to a dictionary type at that spot and that made it work.

Original Code:  
`rackList.append(project.app.res.Rack(rack). __dict__ )`

Working Code:  
`rackList.append(dict(project.app.res.Rack(rack). __dict__ ))`

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [March 18, 2021, 7:02pm UTC](https://forum.inductiveautomation.com/t/ign-5231-jsonencode-issue/44603/24 "2021-03-18T19:02:16Z")

</div>

Here is a decent article on how the built-in ` __dict__ ` attribute works.

> **[The magic behind Attribute Access in Python](https://codesachin.wordpress.com/2016/06/09/the-magic-behind-attribute-access-in-python/)**
>
> Most people know just one thing when it comes to attribute access – the dot ‘.’ (as in x.some\_attribute). In simple terms, attribute access is the way you retrieve an object linke…

---

<div class="post-metadata">

**Author:** ![justin.brzozoski](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justin.brzozoski/32/13452_2.png) [@justin.brzozoski](https://forum.inductiveautomation.com/u/justin.brzozoski)\
**Post date:** [March 19, 2021, 1:31pm UTC](https://forum.inductiveautomation.com/t/ign-5231-jsonencode-issue/44603/25 "2021-03-19T13:31:49Z")

</div>

Thank you for the well-timed thread. I _just_ bumped into this last night in 8.1.3.

In my case some of the nested references deep inside the parameter to jsonEncode were being pulled from Perspective component properties and not coming up in the expected type. I fixed it by finding the spots in the code that pulled in from the Perspective properties and cast everything using dict/list/str there.

---

<div class="post-metadata">

**Author:** ![justin.brzozoski](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justin.brzozoski/32/13452_2.png) [@justin.brzozoski](https://forum.inductiveautomation.com/u/justin.brzozoski)\
**Post date:** [April 5, 2021, 3:48pm UTC](https://forum.inductiveautomation.com/t/ign-5231-jsonencode-issue/44603/26 "2021-04-05T15:48:26Z")

</div>

In case others end up fighting this later and are having trouble finding the bad sub-elements of a bigger structure, this is a quick bit of debug code I put together to show the data types of a structure before passing it into `jsonEncode`:

```python
def type_tree(element, prefix='/'):
    type_string = str(type(element))
    output_string = '{}={}\n'.format(prefix, type_string)
    if hasattr(element, ' __iter__'):
        if hasattr(element, 'keys'):
            for key in element.keys():
                output_string = output_string + type_tree(element[key], '{}[{}]'.format(prefix, key))
        else:
            for x, val in enumerate(element):
                output_string = output_string + type_tree(val, '{}[{}]'.format(prefix, x))
    return output_string

```

---

<div class="post-metadata">

**Author:** ![dhiren.naidoo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dhiren.naidoo/32/19485_2.png) [@dhiren.naidoo](https://forum.inductiveautomation.com/u/dhiren.naidoo)\
**Post date:** [June 7, 2022, 4:36pm UTC](https://forum.inductiveautomation.com/t/ign-5231-jsonencode-issue/44603/27 "2022-06-07T16:36:05Z")

</div>

I know it’s a necro, but I also ran into this and didn’t realise what was going on until this thread. While str() stopped the error, it was a red herring. Thanks for putting me on the right path!

I had PyDocumentAdapter objects sitting inside of a dictionary and jsonEncode doesn’t seem to like having to process this. (I guess I can understand the jsonEncode function not understanding how to cast everything thrown at it).

---

<div class="post-metadata">

**Author:** ![bfreeman](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bfreeman/32/87436_2.png) [@bfreeman](https://forum.inductiveautomation.com/u/bfreeman)\
**Post date:** [February 22, 2023, 2:36pm UTC](https://forum.inductiveautomation.com/t/ign-5231-jsonencode-issue/44603/28 "2023-02-22T14:36:36Z")

</div>

Also getting the issue on a list of dict. I am trying to JSON encode a list with an item filtered out.

```python
custom_list = [{},{}] # list of dict from a custom prop

updated_list = []
for i in custom_list:
  if i["attr"] != "filter_value":
    updated_list.append(i)

# Or
updated_list = filter(lambda i: i["attr"] != "filter_value", custom_list)

system.util.jsonEncode(custom_list) # OK
system.util.jsonEncode(updated_list) # Fails

```

The workaround is to change the append line of code to `updated_list.append(dict(i))`

This is quite disturbing and means that the `filter` function can't be used.

While debugging I noticed that `type(i)` returns the type `com.inductiveautomation.perspective.gateway.script.PropertyTreeScriptWrapper$ObjectWrapper`

Is this a bug or expected behavior?

---

<div class="post-metadata">

**Author:** ![justin.brzozoski](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justin.brzozoski/32/13452_2.png) [@justin.brzozoski](https://forum.inductiveautomation.com/u/justin.brzozoski)\
**Post date:** [February 22, 2023, 2:44pm UTC](https://forum.inductiveautomation.com/t/ign-5231-jsonencode-issue/44603/29 "2023-02-22T14:44:51Z")

</div>

I eventually made up a quick function to "groom" the structures into something that `jsonEncode` wouldn't barf on:

> [@Function to convert json string or python dict into ObjectWrapper](https://forum.inductiveautomation.com/t/function-to-convert-json-string-or-python-dict-into-objectwrapper/32861/16):
>
> Forcing each to the expected type while traversing makes a deep copy: def sanitize\_tree(element): if hasattr(element, '\_\_iter\_\_'): if hasattr(element, 'keys'): return dict((k, sanitize\_tree(element[k])) for k in element.keys()) else: return list(sanitize\_tree(x) for x in element) return element EDIT: Got rid of the unnecessary zip() and used only one keys() iterator instead of two.

[Previous page](https://forum.inductiveautomation.com/t/ign-5231-jsonencode-issue/44603.md?page=1)
