If you have some JSON that will contain an unknown path to something like
...... {
"in": "query",
"name": "@baseType",
"type": "string",
"description": "To route object modelsnValues: xxxuyyy or xxxzzzd"
},
{
"in": "header",
"name": "X-System",
"type": "string",
"default": "Inventory: Inventory\nFor Quickconnect: My Resourse"
},
{
"in": "header",
"name": "env",
"type": "string",
"default": "Environment, i.e: a01, b02, etc."
}
I need to delete all the default lines where "in": "header"
but in my case, I have no idea all the paths that could happen. Any ideas?
If I know the path, then this is relatively easy.
for get_param_rec in api_spec['paths']['/resource']['get']['parameters']:
if get_param_rec['in'] == "header" and "default" in get_param_rec:
logger.info(" found bad header record and deleting it.")
del get_param_rec['default']
2
Answers
You can iterate through the JSON dictionary with a recursive function:
input.json
:output
:You can use a recursive function that removes bad records according to your criteria at each depth level. Normalize each sub-dict to a sequence of values so you can iterate through its sub-records in the same way as a sub-list:
Demo: https://replit.com/@blhsing/DefensiveMadeupService