Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to convert COBOL statement into dynamic Json Structure using Python

#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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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)"
    ]
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading