MCPcopy Create free account
hub / github.com/StackStorm/st2 / json_loads

Function json_loads

st2common/st2common/util/jsonify.py:144–171  ·  view source on GitHub ↗

Given an object, this method tries to json.loads() the value of each of the keys. If json.loads fails, the original value stays in the object. :param obj: Original object whose values should be converted to json. :type obj: ``dict`` :param keys: Optional List of keys whose val

(obj, keys=None)

Source from the content-addressed store, hash-verified

142
143
144def json_loads(obj, keys=None):
145 """
146 Given an object, this method tries to json.loads() the value of each of the keys. If json.loads
147 fails, the original value stays in the object.
148
149 :param obj: Original object whose values should be converted to json.
150 :type obj: ``dict``
151
152 :param keys: Optional List of keys whose values should be transformed.
153 :type keys: ``list``
154
155 :rtype ``dict`` or ``None``
156 """
157 if not obj:
158 return None
159
160 if not keys:
161 keys = list(obj.keys())
162
163 for key in keys:
164 try:
165 obj[key] = json_decode(obj[key])
166 except Exception:
167 # NOTE: This exception is not fatal so we intentionally don't log anything.
168 # Method behaves in "best effort" manner and dictionary value not being JSON
169 # string is perfectly valid (and common) scenario so we should not log anything
170 pass
171 return obj
172
173
174def try_loads(s):

Callers 1

Calls 2

json_decodeFunction · 0.85
keysMethod · 0.80

Tested by

no test coverage detected