Basically I need to set data into a JSON API Payload before I need to send it in the API request.
I have 2 files,
- text file which has JSON payload format, say json.txt
- yml file which has actual data, say tdata.yml
Im writing a python code to create actual payload by inserting data from yml file to JSON payload format
Content of file 1
{
"mpls_routing":
{
"add_novel":
{
"device_ip": "DEVICE_IP",
"discoveryHelloHoldTime": "DISCOVERYHELLOHOLDTIME",
"discoveryHelloInterval": "DISCOVERYHELLOINTERVAL",
"discoveryTargetedHelloHoldTime": "DISCOVERYTARGETEDHELLOHOLDTIME",
"discoveryTargetedHelloInterval": "DISCOVERYTARGETEDHELLOINTERVAL",
"downstreamMaxLabel": "DOWNSTREAMMAXLABEL",
"downstreamMinLabel": "DOWNSTREAMMINLABEL",
"initialBackoff": "INITIALBACKOFF",
"instanceId": "554972419",
"interfaceName": "INTERFACENAME",
"isEntropyLabelEnabled": "ISENTROPYLABELENABLED",
"isExplicitNullEnabled": "ISEXPLICITNULLENABLED",
"isLoopDetectionEnabled": "ISLOOPDETECTIONENABLED",
"isNsrEnabled": "ISNSRENABLED",
"keepAliveInterval": "KEEPALIVEINTERVAL",
"ldpId": "LDPID",
"ldpIpMask": {
"address": ""
},
"ldpPepSettings": [
{
"com.novel.xmp.model.managed.standardTechnologies.ldp.LdpPepSettings": {
"labelDistributionMethod": "DISTMETHOD",
"ldpId": "LDPID",
"name": "MPLSINTERFACE",
"owningEntityId": "OWNINGENTITYID"
}
}
],
"maximumBackoff": "MAXIMUMBACKOFF",
"sessionHoldTime": "SESSIONHOLDTIME"
}
}
This is the yml data file
mpls_interface:
DEVICE_IP: "10.104.120.141"
DISCOVERYHELLOHOLDTIME: "15"
DISCOVERYHELLOINTERVAL: "5"
DISCOVERYTARGETEDHELLOHOLDTIME: "90"
DISCOVERYTARGETEDHELLOINTERVAL: "10"
DOWNSTREAMMAXLABEL: "289999"
DOWNSTREAMMINLABEL: "24000"
INITIALBACKOFF: "15"
INTERFACENAME: "LOOPBACK0"
ISENTROPYLABELENABLED: "FALSE"
ISEXPLICITNULLENABLED: "FALSE"
ISLOOPDETECTIONENABLED: "FALSE"
ISNSRENABLED: "TRUE"
KEEPALIVEINTERVAL: "60"
LDPID: "192.168.0.141"
MAXIMUMBACKOFF: "120"
OWNINGENTITYID: ""
SESSIONHOLDTIME: "180"
DISTMETHOD: "LDP"
MPLSINTERFACE: "TenGigE0/0/0/4"
I have following code which reads the JSON format file data
template_data = file.read()
template_data = json.loads(template_data)
if "add_novel" in template_data:
full_template_file = template_data["add_novel"]
if testcase_name in full_template_file:
template_file_data = full_template_file[testcase_name]
And following code which reads the yml file
with open(self.mpls_yml_file) as stream:
yml_data = yaml.safe_load(stream)
if "mpls_interface" in yml_data:
yml_file_data = yml_data["mpls_interface"]
return yml_file_data
Then I pass both template_file_data and yml_file_data to following code to create the actual API payload.
testcase_data = {key: yml_file_data[value] for key, value in template_file_data.items() for k, v in
yml_file_data.items()}
logger.info("n Testcase data %s" % testcase_data)
return testcase_data
But I’m not able to get the right format since there is nested dictionary and also a array inside a dictionary.
3
Answers
The good news is that I don’t think you are planning to update the nested elements
I am guessing this because the YAML file does not contain anything for
ldpPepSettings
, for example.So you could just use the original JSON information for parts where the YAML file does not provide an update. For example:
could become
You will still have the problem that the two files have different keys. One has lowerCamelCase and the other has ALLCAPS.
You could try to fudge it like this?
The key here is to do the text search/replace to the JSON file’s contents before JSON decode it.
Output:
You need a recursive function that can navigate as deep as necessary in the JSON (Python dictionary) to substitute the values. Something like this:
Output: