Created
February 16, 2022 15:48
-
-
Save amitkrout/396da3095e645d462f09e2aa08f8e079 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import json | |
| import re | |
| def findkeysvalues(inputDict, key): | |
| if isinstance(inputDict, list): | |
| for i in inputDict: | |
| for x in findkeysvalues(i, key): | |
| yield x | |
| if isinstance(inputDict, dict): | |
| if key in inputDict: | |
| yield inputDict[key] | |
| for j in inputDict.values(): | |
| for x in findkeysvalues(j, key): | |
| yield x | |
| def process_JSON_value(jsonFileInput, parentInputKey, key): | |
| with open(jsonFileInput) as jsonFile: | |
| data = json.load(jsonFile) | |
| Dict = { } | |
| for i in data: | |
| if i == parentInputKey: | |
| Dict[i] = data[i] | |
| return list(findkeysvalues(Dict, key)) | |
| def createRulesJSON(): | |
| with open("test1.json") as jsonFile: | |
| data = json.load(jsonFile) | |
| Dict = { } | |
| for p in data: | |
| Dict[p] = {} | |
| rules_items_source = findkeysvalues(data, "source") | |
| for i in rules_items_source: | |
| x = re.findall("\w+", i[0]) | |
| items = process_JSON_value("test.json", x[0], "compname") | |
| for p in data: | |
| Dict[p]['source'] = items | |
| rules_items_source = findkeysvalues(data, "dest") | |
| for i in rules_items_source: | |
| x = re.findall("Microservice.\w+", i[0]) | |
| y = x[0].split(".") | |
| items = process_JSON_value("test.json", y[0], y[1]) | |
| for p in data: | |
| Dict[p]['dest'] = items | |
| rules_items_port = findkeysvalues(data, "port") | |
| for i in rules_items_port: | |
| for p in data: | |
| Dict[p]['port'] = i | |
| with open("sample.json", "w") as file: | |
| json.dump(Dict, file) | |
| print (Dict) | |
| createRulesJSON() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment