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)
| 142 | |
| 143 | |
| 144 | def 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 | |
| 174 | def try_loads(s): |
no test coverage detected