# Test variable type

**URL:** <https://forum.inductiveautomation.com/t/test-variable-type/67326>\
**Category:** Ignition\
**Tags:** ignition81, scripting, perspective\
**Created:** [December 7, 2022, 9:31am UTC](https://forum.inductiveautomation.com/t/test-variable-type/67326 "2022-12-07T09:31:56Z")\
**Posts on this page:** 1\
**Showing post:** 10

<div class="post-metadata">

**Author:** ![tyler.bennett](https://avatars.discourse-cdn.com/v4/letter/t/f04885/32.png) [@tyler.bennett](https://forum.inductiveautomation.com/u/tyler.bennett)\
**Post date:** [April 17, 2024, 7:40am UTC](https://forum.inductiveautomation.com/t/test-variable-type/67326/10 "2024-04-17T07:40:46Z")

</div>

Here is what I have been using to test if a variable is a dict like object or list like object ([lifted from here](https://forum.inductiveautomation.com/t/bug-system-util-sendmessage-fails-with-remoteservers-when-payload-is-not-a-dict/55040/7)):

> [@\[Bug\] system.util.sendMessage fails with remoteServers when payload is not a dict](https://forum.inductiveautomation.com/t/bug-system-util-sendmessage-fails-with-remoteservers-when-payload-is-not-a-dict/55040/7):
>
> ```auto
> def _isDictLikeObject(value):
> return 'iteritems' in dir(value)
> 
> def _isListLikeObject(value):
> return 'append' in dir(value)
> 
> ```

I'm curious if others have recommendation for better/preferred "Duck typing" methodology to detect a dict like objects or list like objects.

I ran into the issue with `DotReferenceJythonMap` types when I used a binding on a Perspective view custom property to create a list of dicts. I then tried to reference each dict in other property bindings with a script transform, attempting to merge with other programmatically created dicts. My custom method to merge dictionaries failed in some weird ways due to the `DotReferenceJythonMap` type.

Here is my previous custom merge function ([lifted from here](https://stackoverflow.com/questions/10703858/python-merge-multi-level-dictionaries)) that uses type checking.

```python
def merge(d1, d2):
	
	import collections 
	
	for k,v2 in d2.items():
		v1 = d1.get(k) # returns None if v1 has no value for this key
		if ( isinstance(v1, collections.Mapping) and 
			isinstance(v2, collections.Mapping) ):
			merge(v1, v2)
		else:
			d1[k] = v2
			
	return d1

```

Here is my revised custom merge function that uses "duck typing".

```python
def merge_test(d1, d2):
	for k,v2 in d2.items():
		v1 = d1.get(k)
		if ( 'iteritems' in dir(v1) and 'iteritems' in dir(v2) ):
			merge(v1, v2)
		else:
			d1[k] = v2
	return d1

```

Not sure if my examples are ideal (I welcome feedback) but the thread was light on actual examples so I thought I'd put these out there..

**One last note...** Notably the `system.util.jsonEncode()` function does not handle the object type `systemcom.inductiveautomation.perspective.gateway.script.DotReferenceJythonMap` type, nor does it handle many (any) of the other dict/list like wrapper objects Ignition like to create.

---

_[View the full topic](https://forum.inductiveautomation.com/t/test-variable-type/67326)._
