Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

719 B
Raw Blame
export function parseIfJSONString<T>(value: unknown): T | undefined {
    if (value == null) {
        return undefined
    }

    if (typeof value === 'string') {
        try {
            return JSON.parse(value) as T
        } catch {
            return undefined
        }
    }

    return value as T
}

export function parseRecordIfJSONString(value: unknown): Record<string, unknown> {
    const parsed = parseIfJSONString<unknown>(value)

    if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
        return {}
    }

    return parsed as Record<string, unknown>
}

export function escapeHogQLString(value: string): string {
    return value.replaceAll('\\', '\\\\').replaceAll("'", "\\'")
}