# Casting Python Dictionary as JSON Object via Script Transform on a Tag Binding

**URL:** <https://forum.inductiveautomation.com/t/casting-python-dictionary-as-json-object-via-script-transform-on-a-tag-binding/47016>\
**Category:** Ignition\
**Tags:** ignition81, scripting, perspective\
**Created:** [May 23, 2021, 2:15am UTC](https://forum.inductiveautomation.com/t/casting-python-dictionary-as-json-object-via-script-transform-on-a-tag-binding/47016 "2021-05-23T02:15:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gschneider](https://avatars.discourse-cdn.com/v4/letter/g/bcef8e/32.png) [@gschneider](https://forum.inductiveautomation.com/u/gschneider)\
**Post date:** [May 23, 2021, 2:15am UTC](https://forum.inductiveautomation.com/t/casting-python-dictionary-as-json-object-via-script-transform-on-a-tag-binding/47016/1 "2021-05-23T02:15:31Z")

</div>

Hello!

I am trying to get data from a UDT (with OPC tags connected to a PLC) to dynamically load interlocks for a perspective view. Part of this UDT has a bool value “Used” which is true when that interlock is being used in the PLC.

To display this, I am trying to bring in the UDT via tag binding (direct for now, indirect later) and use a script transform to filter out the objects where “Used” is false. I have it pretty much figured out, except that my result is just a PyString and not an object and I cannot seem to figure out how to cast this correctly so that I can use each interlock’s object values as params for a view repeater.

Here is the script:

```
            import json
            dict = system.util.jsonDecode(str(value))
            for key in dict:
                            if dict[key]["Used"] == 0:
                                            del dict[key]
            newValue = json.dumps(dict, indent = 4)
            return newValue
```

---

<div class="post-metadata">

**Author:** ![gschneider](https://avatars.discourse-cdn.com/v4/letter/g/bcef8e/32.png) [@gschneider](https://forum.inductiveautomation.com/u/gschneider)\
**Post date:** [May 23, 2021, 2:18am UTC](https://forum.inductiveautomation.com/t/casting-python-dictionary-as-json-object-via-script-transform-on-a-tag-binding/47016/2 "2021-05-23T02:18:07Z")

</div>

Here is a screenshot of the code for more reference:

 ![jsonObjectScreenshot1](https://us1.discourse-cdn.com/inductiveautomation/original/3X/3/c/3c9f254fd35ecc5002260522af44af6322be85da.png)

Here is the result of that script on the binding:  
 ![jsonObjectScreenshot2](https://us1.discourse-cdn.com/inductiveautomation/original/3X/a/6/a60e3954890e4e52720fab7cf40cbc49a24de00d.png)

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [May 23, 2021, 3:34am UTC](https://forum.inductiveautomation.com/t/casting-python-dictionary-as-json-object-via-script-transform-on-a-tag-binding/47016/3 "2021-05-23T03:34:07Z")

</div>

```auto
    my_dict = system.util.jsonDecode(str(value))
    for key in my_dict:
        if my_dict[key]["Used"] == 0:
            del my_dict[key]
    return my_dict

```

1. In your example, `dict` is already a format which will be recognized as an object in Perspective, but…
2. You shouldn’t name you variable `dict` because `dict` is essentially a type in python, which makes it a reserved word. You can usually do this without issue, but it could also cause problems which can be hard to troubleshoot because you think you’re looking at a variable when you could perhaps be creating a whole new dict. In my example, I replaced your `dict` variable with `my_dict` to avoid shadowing the `dict` keyword.

---

<div class="post-metadata">

**Author:** ![gschneider](https://avatars.discourse-cdn.com/v4/letter/g/bcef8e/32.png) [@gschneider](https://forum.inductiveautomation.com/u/gschneider)\
**Post date:** [May 23, 2021, 11:19am UTC](https://forum.inductiveautomation.com/t/casting-python-dictionary-as-json-object-via-script-transform-on-a-tag-binding/47016/4 "2021-05-23T11:19:09Z")

</div>

Thanks for the reply! This worked to solve the initial problem, however I actually need an array (I thought I needed an object but I was wrong).

![jsonObjectScreenshot4](https://us1.discourse-cdn.com/inductiveautomation/original/3X/4/3/43c771bbfd0552440845f06e4887ce0554c6a28c.png)

I had originally named each object 0 through 6, but the python dict was not a fan so I added an “i” in front.

---

<div class="post-metadata">

**Author:** ![gschneider](https://avatars.discourse-cdn.com/v4/letter/g/bcef8e/32.png) [@gschneider](https://forum.inductiveautomation.com/u/gschneider)\
**Post date:** [May 23, 2021, 11:30am UTC](https://forum.inductiveautomation.com/t/casting-python-dictionary-as-json-object-via-script-transform-on-a-tag-binding/47016/5 "2021-05-23T11:30:17Z")

</div>

Ok, after some google searching and experimenting, I just needed to declare it as a list via:

list(my\_dict.values())

Thanks for your help!!

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [May 23, 2021, 3:15pm UTC](https://forum.inductiveautomation.com/t/casting-python-dictionary-as-json-object-via-script-transform-on-a-tag-binding/47016/6 "2021-05-23T15:15:56Z")

</div>

```auto
return my_dict.items()

```

Will return a list of your objects, but if you just need the values they contain, then yes, `my_dict.values()` will work, and you shouldn’t need to wrap it in a list.

```auto
return my_dict.values()

```
