605509ff07512189e25e2f4b45250855f1dbd3a6
[testsuite/python-testing-utils.git] / robotframework-onap / eteutils / JSONUtils.py
1 import json
2
3 from deepdiff import DeepDiff
4
5
6 class JSONUtils:
7     """JSONUtils is common resource for simple json helper keywords."""
8
9     def __init__(self):
10         pass
11
12     def json_equals(self, left, right):
13         """JSON Equals takes in two strings or json objects, converts them into json if needed and then compares them,
14         returning if they are equal or not."""
15         if isinstance(left, str) or isinstance(left, unicode):
16             left_json = json.loads(left)
17         else:
18             left_json = left
19         if isinstance(right, str) or isinstance(right, unicode):
20             right_json = json.loads(right)
21         else:
22             right_json = right
23             
24         ddiff = DeepDiff(left_json, right_json, ignore_order=True)
25         if ddiff == {}:
26             return True
27         else:
28             return False
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
38     
39     def find_element_in_array(self, searchedArray, key, value):
40         """ Takes in an array and a key value, it will return the items in the array that has a key and value that
41         matches what you pass in """
42         elements = []
43         for item in searchedArray:
44             if key in item:
45                 if item[key] == value:
46                     elements.append(item)
47         return elements