Problem
You are given a string s that is intended to be a JSON text. Implement a function isValidJsonStructure(s) -> bool that checks structural validity of the JSON with the following simplified rules:
Rules to validate
-
After ignoring leading/trailing whitespace, the text must start and end with matching outer brackets:
-
either
{ ... }
or
[ ... ]
.
-
Brackets must be
properly nested and balanced
:
-
curly braces
{}
-
square brackets
[]
-
JSON strings are delimited by
double quotes
"..."
.
-
While inside a string, any bracket characters (
{ } [ ]
) must be ignored for nesting purposes.
-
A quote preceded by an
odd
number of backslashes is escaped (e.g.,
\"
closes? no;
\"
contains an escaped quote). In other words,
"
does
not
end a string.
-
You do
not
need to validate JSON key/value grammar (colons, commas, numbers,
true/false/null
, etc.). Only validate the above structural properties.
Input/Output
-
Input:
string
s
-
Output:
true
if structurally valid per the rules; otherwise
false
.
Constraints
Examples
-
"{\"a\":[1,2,3]}"
→
true
-
"{[}]"
→
false
(bad nesting)
-
"{\"x\":\"[}\"}"
→
true
(brackets inside a string are ignored)
-
"{\"a\":\"unterminated}"
→
false
(unclosed string)