1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

json_query: Handle AnsibleUnicode, AnsibleUnsafeText (#1434)

jmespath library does not undestand custom string types
such as AnsibleUnicode, and AnsibleUnsafeText.
So user need to use ``to_json | from_json`` filter while using
functions like ``starts_with`` and ``contains`` etc.
This hack will allow user to get rid of this filter.

Fixes: #320

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
Abhijeet Kasurde 2020-12-04 11:54:42 +05:30 committed by GitHub
parent 47371041c7
commit 5319437bc2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 0 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- json_query - handle ``AnsibleUnicode`` and ``AnsibleUnsafeText`` (https://github.com/ansible-collections/community.general/issues/320).

View file

@ -35,6 +35,9 @@ def json_query(data, expr):
raise AnsibleError('You need to install "jmespath" prior to running ' raise AnsibleError('You need to install "jmespath" prior to running '
'json_query filter') 'json_query filter')
# Hack to handle Ansible String Types
# See issue: https://github.com/ansible-collections/community.general/issues/320
jmespath.functions.REVERSE_TYPES_MAP['string'] = jmespath.functions.REVERSE_TYPES_MAP['string'] + ('AnsibleUnicode', 'AnsibleUnsafeText', )
try: try:
return jmespath.search(expr, data) return jmespath.search(expr, data)
except jmespath.exceptions.JMESPathError as e: except jmespath.exceptions.JMESPathError as e: