Skip to content

Bug: JSON output contains unescaped control characters from Notion API #3

Description

@chen-gdp

Bug Description

When running notion db query with certain results, the JSON output contains unescaped newline characters, causing JSON parsing to fail.

Root Cause

Some Notion pages have URLs that contain actual newline characters. When json.dumps outputs the JSON with indent=2, the newlines in string values are not being properly escaped.

Evidence

When running through pytest capture, the output becomes corrupted:

json.decoder.JSONDecodeError: Invalid control character at: line 43 column 81 (char 1370)

The raw JSON contains unescaped newlines in URL values.

Suggested Fix

Sanitize string values before JSON output to remove or escape problematic control characters:

def _sanitize_json_value(value):
    """Remove or escape control characters that break JSON parsing."""
    if isinstance(value, str):
        # Replace problematic control characters
        return value.replace('\n', '\\n').replace('\r', '\\r').replace('\t', '\\t')
    return value

Or use json.dumps with escapechar parameter, or post-process the output.

File to Fix

src/notion_cli/core/output.py

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions