Spaces:
Build error
Build error
| """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) | |