I am trying to build a Tree based on data to be found in a dictionary. I know that this could be achieved using recursion, but I am facing troubles solving this problem.
The goal would be to construct a Tree in the form of:
class Tree():
def __init__(self, data) -> None:
self.data = data
self.children = []
def addChildren(self, child):
self.children.append(child)
The following dictionary should be "parsed" and represented by a Tree.
{
"PaymentCoverageRequestResource":{
"required":[
"accountId",
"paymentCoverageRequestId"
],
"type":"object",
"properties":{
"paymentCoverageRequestId":{
"maxLength":35,
"type":"string",
"description":"Identification of the payment Coverage Request"
},
"payee":{
"maxLength":70,
"type":"string",
"description":"The merchant where the card is accepted as information to the PSU."
},
"instructedAmount":{
"type":"object",
"properties":{
"amount":{
"maxLength":70,
"type":"string",
"description":"The merchant where the card is accepted as information to the PSU."
},
"currency":{
"maxLength":3,
"type":"string",
"description":"The merchant where the card is accepted as information to the PSU."
}
}
},
"accountId":{
"$ref":"another reference"
}
},
"description":"Payment coverage request structure.nThe request must rely either on a cash account or a payment card.nThe [instructedAmount] property is the payment account on wihich the request is processed. This amount must be positive.nAmounts must always be set as positive values.n",
"example":"{n "paymentCoverageRequestId" : "MyCoverage123456",n "instructedAmount" : {n "amount" : 12345.0,n "currency" : "EUR"n },n "accountId" : {n "iban" : "YY13RDHN98392489481620896668799742"n }n}",
"x-definition-type":"Resources"
}
}
A node is considered to be a parent-node, if and only if it contains the key-value pair
"type":"object"
Its children are all the key-value pairs found in "properties".
I tried to solve this problem using the following code, but it am lacking some experience.
def parse_json(requestBody : dict):
def parse_json_rec(tree : Tree, requestBody : dict):
#something ?
if len(requestBody) == 0:
return tree
(k, v), = requestBody.items()
requestBody.pop(k)
print(k)
if v['type'] == 'object': # OR ARRAY
parent_node = Tree(k)
print(v)
return parse_json_rec(tree.addChildren(parse_json_rec(parent_node, v), requestBody))
else:
leaf_node = Tree(k)
tree.addChildren(leaf_node)
return parse_json_rec(tree, requestBody)
pass
root = Tree("root")
return parse_json_rec(root, requestBody)
The result I would like to achieve is a Tree containing multiple nodes as represented here:
PaymentCoverageRequestResource
paymentCoverageRequestId
payee
instructedAmount
amount
currency
accountId
2
Answers
There are a couple issues I see that might be causing you trouble are:
Consider instead, just making a loop over the properties instead of doing recursion on the same level, I think it’d be easier to understand.
I reasonably created a package AbstractTree which can solve this:
The output looks like this: