.jsonDecode Filter

Any idea how to filter dictionaries? I am using the function system.net.httpGet to get a JSON string then converting it into a dictionary using system.util.jsonDecode(). I get the following results:

[…]
[{u’value’: 3.6377865516, u’timestamp’: 1498867200, u’realDate’: ‘2017-07-01 12:00:00’},
{u’value’: None, u’timestamp’: 1498868100}, {u’value’: None, u’timestamp’: 1498869000},
{u’value’: None, u’timestamp’: 1498869900}, {u’value’: None, u’timestamp’: 1498870800},
{u’value’: None, u’timestamp’: 1498871700}, {u’value’: None, u’timestamp’: 1498872600},
[…]

I need to work with this data later on, so I was wondering how I can filter it and get only the values that are different than None. Pretty much the first row.

I am pretty new to ignition and Python, so I apologize in advance for my lack of knowledge.

Thanks in advance.

Python has a built-in filter function, that you could use like this:

original = system.util.jsonDecode(...)
filtered = filter(lambda x: x['value'] is not None, original)

This should leave you with a list of only dictionaries that had a ‘value’ key defined.

https://docs.python.org/2/library/functions.html#filter

You could also try:

original = system.util.jsonDecode(...)
filtered = [x for x in original if x['value'] is not None]
1 Like