File size: 1,422 Bytes
9328e91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""JSON utility functions for error visualization and correction."""

import json


def find_and_visualize_json_errors(json_string):
    """Find JSON syntax errors and insert newlines at error positions.

    This helps visualize where JSON parsing errors occur by adding
    line breaks at problematic positions.

    Args:
        json_string: The JSON string to analyze

    Returns:
        String with newlines inserted at error positions
    """
    error_positions = []
    start = 0
    while start < len(json_string):
        try:
            json.loads(json_string[start:])
            break  # If no error, we're done
        except json.JSONDecodeError as e:
            position = start + e.pos
            if not error_positions or position > error_positions[-1] + 10:
                error_positions.append(position)
            start = position + 1

    # Insert newlines at error positions
    result = ""
    last_pos = 0
    for pos in error_positions:
        result += json_string[last_pos:pos] + "\n"
        last_pos = pos
    result += json_string[last_pos:]

    return result


def join_lines_after_correct_json_errors(lines, separator='\n'):
    """Join lines back together after JSON error correction.

    Args:
        lines: List of line strings
        separator: Separator to use when joining (default: newline)

    Returns:
        Joined string
    """
    return separator.join(lines)