Skip to content

Instantly share code, notes, and snippets.

@amitkrout
Created February 22, 2022 02:48
Show Gist options
  • Select an option

  • Save amitkrout/63878e1559dc3ada2522134093198e02 to your computer and use it in GitHub Desktop.

Select an option

Save amitkrout/63878e1559dc3ada2522134093198e02 to your computer and use it in GitHub Desktop.
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 = { }
# rules_items_source = list(findkeysvalues(data, "source"))
for p in data:
rules_items_source = list(findkeysvalues(data, "source"))
Dict[p] = { }
for i in rules_items_source:
x = re.findall("\w+", i[0])
sourceItems = process_JSON_value("test.json", x[0], "compname")
Dict[p]['source'] = sourceItems
print(Dict)
rules_items_dest = list(findkeysvalues(data, "dest"))
for p in data:
count = 0
for i in rules_items_dest:
x = re.findall("Microservice.\w+", i[0])
y = x[0].split(".")
items = process_JSON_value("test.json", y[0], y[1])
Dict[p]['dest'] = items
count = count +1
rules_items_port = findkeysvalues(data, "port")
for i in rules_items_port:
for p in data:
Dict[p]['port'] = i
print(Dict)
with open("sample.json", "w") as file:
json.dump(Dict, file)
createRulesJSON()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment