# How to parse null parameters in scripts

**URL:** <https://forum.inductiveautomation.com/t/how-to-parse-null-parameters-in-scripts/100552>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [January 7, 2025, 6:27pm UTC](https://forum.inductiveautomation.com/t/how-to-parse-null-parameters-in-scripts/100552 "2025-01-07T18:27:50Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Noah\_Gilgo](https://avatars.discourse-cdn.com/v4/letter/n/c89c15/32.png) [@Noah\_Gilgo](https://forum.inductiveautomation.com/u/Noah_Gilgo)\
**Post date:** [January 7, 2025, 6:27pm UTC](https://forum.inductiveautomation.com/t/how-to-parse-null-parameters-in-scripts/100552/1 "2025-01-07T18:27:50Z")

</div>

I'm trying to write a script transform that will take the JSON below and delete any leaf nodes with `{label: null}`.

```python
[
  {
    "icon": {
      "path": "XXX",
      "style": {},
      "color": ""
    },
    "expanded": true,
    "label": "XXX",
    "data": {
      "EqType": XXX,
      "EqEnabled": true,
      "EqName": "XXX",
      "EqPath": "XXX",
      "EqID": 1
    },
    "items": [
      {
        "icon": {
          "path": "XXX/XXX",
          "style": {},
          "color": ""
        },
        "expanded": true,
        "label": "XXX",
        "data": {
          "EqType": "XXX",
          "EqEnabled": true,
          "EqName": "XXX",
          "EqPath": "XXX/XXX",
          "EqID": 1
        },
        "items": [
          {
            "icon": {
              "path": "XXX/XXX",
              "style": {},
              "color": "var(--error)"
            },
            "expanded": false,
            "label": "QA Test Area",
            "data": {
              "EqType": "XXX",
              "EqEnabled": false,
              "EqName": "QA Test Area",
              "EqPath": "XXX/XXX/XXX",
              "EqID": 11
            },
            "items": [
              {
                "icon": {
                  "path": "material/block",
                  "style": {},
                  "color": "var(--error)"
                },
                "expanded": false,
                "label": null,
                "data": {
                  "EqType": "XXX",
                  "EqEnabled": null,
                  "EqName": null,
                  "EqPath": null,
                  "EqID": null
                },
                "items": []
              }
            ]
          }
        ]
      }
    ]
  }
]

```

However, I have not been able to figure out a way to write a Python conditional that checks if `label: null`. This is what I attempted to do in the if statement below:

```python
def transform(self, value, quality, timestamp):
	for enterprise in value:
		for site in enterprise["items"]:
			for area in site["items"]:
				for line in area["items"]:
					if line["label"] == None:
						del line

```

If someone could tell me how to check if a perspective property is null in Python, that would be very helpful.

---

<div class="post-metadata">

**Author:** ![danieltalbot](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/danieltalbot/32/11201_2.png) [@danieltalbot](https://forum.inductiveautomation.com/u/danieltalbot)\
**Post date:** [January 7, 2025, 6:31pm UTC](https://forum.inductiveautomation.com/t/how-to-parse-null-parameters-in-scripts/100552/2 "2025-01-07T18:31:46Z")

</div>

try

if line["label"] is None:

or

if not line["label"]:

---

<div class="post-metadata">

**Author:** ![kvane](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kvane/32/22228_2.png) [@kvane](https://forum.inductiveautomation.com/u/kvane)\
**Post date:** [January 7, 2025, 6:34pm UTC](https://forum.inductiveautomation.com/t/how-to-parse-null-parameters-in-scripts/100552/3 "2025-01-07T18:34:50Z")

</div>

You should review the error(s) you are seeing closely, I am guessing you are seeing things like:

name 'null' is not defined, etc.

You are not dealing with a None, you are dealing with a variable written as "null", if you want it to be treated as None then set that variable at the start of the script:

null = None

This is also true for variables like "true" because Python's true bool is: True

---

<div class="post-metadata">

**Author:** ![Noah\_Gilgo](https://avatars.discourse-cdn.com/v4/letter/n/c89c15/32.png) [@Noah\_Gilgo](https://forum.inductiveautomation.com/u/Noah_Gilgo)\
**Post date:** [January 7, 2025, 6:42pm UTC](https://forum.inductiveautomation.com/t/how-to-parse-null-parameters-in-scripts/100552/4 "2025-01-07T18:42:31Z")

</div>

I'm not getting any errors when I run the script. My problem is that I would like for the script to delete any nodes with `label: null` from the json tree, but the script is not doing so, instead returning the initial json tree with zero modifications whatsoever.

---

<div class="post-metadata">

**Author:** ![kvane](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kvane/32/22228_2.png) [@kvane](https://forum.inductiveautomation.com/u/kvane)\
**Post date:** [January 7, 2025, 6:45pm UTC](https://forum.inductiveautomation.com/t/how-to-parse-null-parameters-in-scripts/100552/5 "2025-01-07T18:45:41Z")

</div>

Did you remove the return at the end of the transform?

> **[Script Transform | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/ignition-modules/perspective/working-with-perspective-components/bindings-in-perspective/transforms/script-transform)**
>
> Script Transforms are special functions that are applied only on an existing binding. They take the result of a binding (or transform) as an input and produce a single output. This will allow you to manipulate a binding result using whatever python...

> It is assumed the custom script will end with a Return comment that is a single output value, dataset, document, or other type of data.

---

<div class="post-metadata">

**Author:** ![Noah\_Gilgo](https://avatars.discourse-cdn.com/v4/letter/n/c89c15/32.png) [@Noah\_Gilgo](https://forum.inductiveautomation.com/u/Noah_Gilgo)\
**Post date:** [January 7, 2025, 6:54pm UTC](https://forum.inductiveautomation.com/t/how-to-parse-null-parameters-in-scripts/100552/6 "2025-01-07T18:54:54Z")

</div>

I've ensured that there is a return statement at the end of the transform. I also cloned `value` into the return variable `tree` to ensure that the code is not returning the original `value` variable.

```python
def transform(self, value, quality, timestamp):
	tree = value[:]
	
	for enterprise in tree:
		for site in enterprise["items"]:
			for area in site["items"]:
				for line in area["items"]:
					if line["label"] == None:
						del line
	
	return tree

```

Unfortunately. I'm still getting the same behavior. `tree` still contains the item

```python
{
  "icon": {
    "path": "material/block",
    "style": {},
    "color": "var(--error)"
  },
  "expanded": false,
  "label": null,
  "data": {
    "EqType": "line",
    "EqEnabled": null,
    "EqName": null,
    "EqPath": null,
    "EqID": null
  },
  "items": []
}

```

with `label: null` that I am trying to remove from the json tree.

---

<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:** [January 7, 2025, 7:02pm UTC](https://forum.inductiveautomation.com/t/how-to-parse-null-parameters-in-scripts/100552/7 "2025-01-07T19:02:07Z")

</div>

What @danieltalbot said.  
JSON defines "a key with no value" with the keyword `null` (not in quotes).  
Python defines the same concept as a singleton object automatically imported as `None`.  
The idiomatic way to check whether something is null/not present _in Python_ is with `if x is None:` or `if x is not None:`.

---

<div class="post-metadata">

**Author:** ![Noah\_Gilgo](https://avatars.discourse-cdn.com/v4/letter/n/c89c15/32.png) [@Noah\_Gilgo](https://forum.inductiveautomation.com/u/Noah_Gilgo)\
**Post date:** [January 7, 2025, 7:09pm UTC](https://forum.inductiveautomation.com/t/how-to-parse-null-parameters-in-scripts/100552/8 "2025-01-07T19:09:34Z")

</div>

Like this?

```python
def transform(self, value, quality, timestamp):
	tree = value[:]
	
	for enterprise in tree:
		for site in enterprise["items"]:
			for area in site["items"]:
				for line in area["items"]:
					label = line["label"]
					if label is None:
						del line
	
	return tree

```

I attempted this, but the code is acting the same as before. It is not deleting the `line` node with `label: null` from `tree`.

---

<div class="post-metadata">

**Author:** ![danieltalbot](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/danieltalbot/32/11201_2.png) [@danieltalbot](https://forum.inductiveautomation.com/u/danieltalbot)\
**Post date:** [January 7, 2025, 7:19pm UTC](https://forum.inductiveautomation.com/t/how-to-parse-null-parameters-in-scripts/100552/9 "2025-01-07T19:19:10Z")

</div>

I'm guessing that area["items"] is an array of line objects.

Try this:

```python
def transform(self, value, quality, timestamp):
	tree = value[:]
	
	for enterprise in tree:
		for site in enterprise["items"]:
			for area in site["items"]:
				for i, line in enumerate(area["items"]):
					label = line["label"]
					if label is None:
						del area[i]
	
	return tree

```

---

<div class="post-metadata">

**Author:** ![Noah\_Gilgo](https://avatars.discourse-cdn.com/v4/letter/n/c89c15/32.png) [@Noah\_Gilgo](https://forum.inductiveautomation.com/u/Noah_Gilgo)\
**Post date:** [January 7, 2025, 7:32pm UTC](https://forum.inductiveautomation.com/t/how-to-parse-null-parameters-in-scripts/100552/10 "2025-01-07T19:32:28Z")

</div>

I attempted your code, and it seems to be attempting to perform the deletion, thankfully.

However, the script errors out because the del statement throws the error `TypeError: 'com.inductiveautomation.perspective.gateway.script.DotReferenceJythonMap' object doesn't support item deletion`

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [January 7, 2025, 8:37pm UTC](https://forum.inductiveautomation.com/t/how-to-parse-null-parameters-in-scripts/100552/11 "2025-01-07T20:37:29Z")

</div>

You need to make a copy of the tree, probably recursively, where you simply do not copy the items you would otherwise delete.

---

<div class="post-metadata">

**Author:** ![Noah\_Gilgo](https://avatars.discourse-cdn.com/v4/letter/n/c89c15/32.png) [@Noah\_Gilgo](https://forum.inductiveautomation.com/u/Noah_Gilgo)\
**Post date:** [January 7, 2025, 8:56pm UTC](https://forum.inductiveautomation.com/t/how-to-parse-null-parameters-in-scripts/100552/12 "2025-01-07T20:56:59Z")

</div>

So far, the only working solution I've managed is by converting `value` from a `JsonifiableArrayList` to a `List[Dict]`, then using a `line_index` variable and the `pop()` method to remove the line from the area.

```python
def transform(self, value, quality, timestamp):
	import json
	tree = value[:][0].toString()
	tree = [json.loads(tree)]
	
	for enterprise in tree:
		for site in enterprise["items"]:
			for area in site["items"]:
				line_index = 0
				for line in area["items"]:
					label = line["label"]
					if label is None:
						area["items"].pop(line_index)
					line_index += 1
	
	return tree

```

For some reason, the `del` keyword does not remove the line from the `items` list, but `pop()` does.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [January 9, 2025, 10:21am UTC](https://forum.inductiveautomation.com/t/how-to-parse-null-parameters-in-scripts/100552/13 "2025-01-09T10:21:42Z")

</div>

```python
def transform(self, value, quality, timestamp):
	for enterprise in value:
		for site in enterprise["items"]:
			for area in site["items"]:
				area['items'] = filter(lambda l: l['label'] is not None, area['items'])
				# area['items'] = [l for l in area['items'] if l['label'] is not None]
	return value

```

Didn't try it but I believe it should do what you want.  
In comment, a comprehension version if that looks clearer to you.
