sv764t | 8bf4998 | 2018-10-25 01:42:52 -0400 | [diff] [blame^] | 1 | import json |
| 2 | |
| 3 | from deepdiff import DeepDiff |
| 4 | |
| 5 | class JSONUtils: |
| 6 | """JSONUtils is common resource for simple json helper keywords.""" |
| 7 | |
| 8 | def json_equals(self, left, right): |
| 9 | """JSON Equals takes in two strings or json objects, converts them into json if needed and then compares them, returning if they are equal or not.""" |
| 10 | if isinstance(left, basestring): |
| 11 | left_json = json.loads(left); |
| 12 | else: |
| 13 | left_json = left; |
| 14 | if isinstance(right, basestring): |
| 15 | right_json = json.loads(right); |
| 16 | else: |
| 17 | right_json = right; |
| 18 | |
| 19 | ddiff = DeepDiff(left_json, right_json, ignore_order=True); |
| 20 | if ddiff == {}: |
| 21 | return True; |
| 22 | else: |
| 23 | return False; |
| 24 | |
| 25 | def json_escape(self, jsonObject): |
| 26 | jsonstr = json.dumps(jsonObject) |
| 27 | outstr = jsonstr.replace('"', '\\"').replace('\n', '\\n') |
| 28 | return outstr |
| 29 | |
| 30 | def make_list_into_dict(self, listOfDicts, key): |
| 31 | """ Converts a list of dicts that contains a field that has a unique key into a dict of dicts """ |
| 32 | d = {} |
| 33 | if isinstance(listOfDicts, list): |
| 34 | for thisDict in listOfDicts: |
| 35 | v = thisDict[key] |
| 36 | d[v] = thisDict |
| 37 | return d |