From 5e3674685140ecfd57d7797b784ea8b95811eba3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Mart=C3=ADnez?= Date: Fri, 20 Mar 2026 08:28:22 +0100 Subject: [PATCH] fix: unwrap string-wrapped empty containers and double-quoted values MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - "{ }" / "[ ]" → {} / [] (Grafana stores empty objects/arrays as strings) - ""value"" → "value" (double outer quotes from re-serialisation) Both passes added to grafanaDeepClean after the unquoteJsonStringValues step. Co-Authored-By: Claude Sonnet 4.6 --- src/extension.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/extension.ts b/src/extension.ts index 1f3798a..46db31e 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -347,6 +347,18 @@ function grafanaDeepClean(s: string): string { // Second trailing-slash pass after unquoting (new content may have been revealed). result = result.replace(/"([^"/]*)\/+"/g, '"$1"'); + // Unwrap string-wrapped empty objects/arrays (with optional inner whitespace): + // "{ }" → {} "[ ]" → [] + // Grafana sometimes stores empty containers as the string value "{ }" / "[ ]". + result = result.replace(/"(\{\s*\})"/g, '$1'); + result = result.replace(/"(\[\s*\])"/g, '$1'); + + // Remove double outer quotes around string values: ""value"" → "value" + // Happens when Grafana double-wraps content (e.g. after re-serialisation). + // The pattern requires 4 consecutive quote chars, so bare "" (empty string) is + // left untouched (only two consecutive quotes, no inner content with extra wrapping). + result = result.replace(/""([^"\\]*)""/g, '"$1"'); + // Strip outer quotes wrapping the whole document: // "{"key":"val"}" → {"key":"val"} // "["item"]" → ["item"]