e5de18285b396578347317bacc73fa6a5370693c
[testsuite/python-testing-utils.git] / robotframework-onap / eteutils / JSONUtils.py
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, str):
11             left_json = json.loads(left);
12         else:
13             left_json = left;
14         if isinstance(right, str):
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 make_list_into_dict(self, listOfDicts, key):
26         """ Converts a list of dicts that contains a field that has a unique key into a dict of dicts """
27         d = {}
28         if isinstance(listOfDicts, list):
29             for thisDict in listOfDicts:
30                 v = thisDict[key]
31                 d[v] = thisDict
32         return d
33     
34     def find_element_in_array(self, searchedArray, key, value):
35         """ Takes in an array and a key value, it will return the items in the array that has a key and value that matches what you pass in """
36         elements = [];
37         for item in searchedArray:
38             if key in item:
39                 if item[key] == value:
40                     elements.append(item);
41         return elements;