from typing import Any
from typing import Callable


def traverse_json_entries(data: Any, callback: Callable) -> int:
    """
    Traverse json and execute callback for each top-level entry

    Could later be optimized by accepting json uri instead of the full object
    and using streaming request/parse APIs to exit early on validation error

    Returns entries count if succeeded
    """

    entries_count = 0

    if isinstance(data, dict):
        for k, v in data.items():
            entries_count += 1
            callback(k, v)

    elif isinstance(data, list):
        for v in data:
            entries_count += 1
            callback(None, v)

    return entries_count