skip to Main Content

i have to convert cobal statement to json

import re
import json

def extract_cursor(cobol_statement):
    pattern = r"MODIFY MAP (?: CURSOR|TEMP|FOR|PERM|CURSOR)?(?:CURSOR)?(?: AT)?(?: FIELD)?(?: DFLD)?s+([w-]+)(?!()"
    action_regex = re.compile(pattern, re.IGNORECASE)
    action_match = action_regex.search(cobol_statement)
    if action_match:
        cursor_value = action_match.group(1)
        if cursor_value.upper() != "FIELD" and cursor_value.upper() != "DFLD":
            result = {
                "cursor": cursor_value,
                "for": None,
                "attributes": None
            }
            return result
    return None

# Example COBOL statements
cobol_statement1 = "MODIFY MAP TEMP CURSOR AT NBS-DS-01"
cobol_statement2 = "MODIFY MAP CURSOR NBS-DS-01"
cobol_statement3 = "MODIFY MAP FOR FIELD NBS-DS-01"
cobol_statement4 = "MODIFY MAP PERM NBS-DS-01"

# Extract cursor from each COBOL statement
cursor_info1 = extract_cursor(cobol_statement1)
cursor_info2 = extract_cursor(cobol_statement2)
cursor_info3 = extract_cursor(cobol_statement3)
cursor_info4 = extract_cursor(cobol_statement4)

# Convert results to JSON format
json_result1 = json.dumps(cursor_info1, indent=4)
json_result2 = json.dumps(cursor_info2, indent=4)
json_result3 = json.dumps(cursor_info3, indent=4)
json_result4 = json.dumps(cursor_info4, indent=4)

print(json_result1)
print(json_result2)
print(json_result3)
print(json_result4)


my third statement have wrong output please some one help i need the out put like
{
"cursor": "NBS-DS-01",
"for": null,
"attributes": null
}

2

Answers


  1. It seems your regex handling the case where FIELD follows after the FOR. The negative lookahead is not necessary here.

    pattern = r"MODIFY MAP (?:TEMP|PERM)?s*(?:CURSOR)?s*(?:AT)?s*(?:FIELD)?s*(?:DFLD)?s+([w-]+)"
    
    Login or Signup to reply.
  2. To convert Cobol statements to JSON, the provided python code uses regex to extract specific parts of the Cobol statement and then builds a JSON object with this info. For example, it looks for the cursor names in Cobol statements and ignores certain keywords. If it successfully finds a cursor name that’s not ‘FIELD’ or ‘DFLD’, it forms a simple JSON with ‘cursor’ and placeholders for ‘for’ and ‘attributes’. This is great for parsing specific data patterns in Cobol and converting them to a more universally readable format like JSON. Just remember, this script is specialized for certain Cobol patterns!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search