#Example COBOL statement
cobol_statement = "MODIFY MAP CURSOR AT NBS-DS-01(1) FOR FIELD (NBS-DS-01, NBS-DS-02(2), NBS-DS-03(3))"
I need a json structure to covert this COBOL i need a result as
{"cursor": "NBS-DS-01(1)", "for": ["NBS-DS-01","NBS-DS-02(2)","NBS-DS-03(3)"]}
I need the json structure to be dynamic whatever the field I’am adding to be convert automatically
>Solution :
You can try this regular expression. If you use a different format you need to reconstruct the regex.
import re
import json
def cobol_to_json(statement):
pattern = r"MODIFY MAP CURSOR AT ([\w-]+\(\d+\)) FOR FIELD \(([\w-]+(?:\(\d+\))?(?:, [\w-]+(?:\(\d+\))?)*)\)"
match = re.match(pattern, statement)
if match:
cursor = match.group(1)
fields_str = match.group(2)
fields = [field.strip() for field in fields_str.split(',')]
result = {
"cursor": cursor,
"for": fields
}
return json.dumps(result, indent=4)
else:
return "invalid statement"
statement = "MODIFY MAP CURSOR AT NBS-DS-01(1) FOR FIELD (NBS-DS-01, NBS-DS-02(2), NBS-DS-03(3))"
result = cobol_to_json(statement)
print(result)
Output:
{
"cursor": "NBS-DS-01(1)",
"for": [
"NBS-DS-01",
"NBS-DS-02(2)",
"NBS-DS-03(3)"
]
}