blob: 5df1e5c739b2817a21fe4bd1b59cefc4759a7fef [file] [log] [blame]
sv764t8bf49982018-10-25 01:42:52 -04001import json
2
3from deepdiff import DeepDiff
4
5class 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