JSONPath Bracket Notation ($["key"])
Learn JSONPath bracket notation for accessing properties with special characters, spaces, or dynamic keys. Includes syntax comparison with dot notation.
Detailed Explanation
Bracket Notation in JSONPath
Bracket notation uses square brackets and quoted strings to access object properties. It is the more versatile alternative to dot notation and is required when property names contain special characters.
Syntax
$["propertyName"]
$["parent"]["child"]
When to Use Bracket Notation
Given this JSON:
{
"first-name": "Alice",
"last name": "Smith",
"2024-revenue": 50000,
"tags": ["developer", "speaker"]
}
$["first-name"]returns"Alice"(hyphen in key)$["last name"]returns"Smith"(space in key)$["2024-revenue"]returns50000(starts with a digit)
None of these would work with dot notation because the property names are not valid JavaScript identifiers.
Combining with Dot Notation
You can freely mix both notations in a single expression:
$.users[0]["first-name"]
$.config["api-keys"].production
Multiple Keys (Union)
Bracket notation also supports selecting multiple properties at once:
$["first-name", "last name"]
This returns both values in a single result array.
Key Considerations
- Always use quotes — single or double quotes around the property name, depending on the implementation.
- Escaped quotes — if the property name itself contains quotes, escape them:
$["say \"hello\""]. - Interchangeable with dot notation — for simple names,
$.nameand$["name"]are equivalent.
Bracket notation ensures you can access any property regardless of its naming conventions, making it indispensable when working with real-world JSON data that may not follow strict naming rules.
Use Case
Use bracket notation when working with JSON from external systems, third-party APIs, or legacy databases where property names contain hyphens, spaces, dots, or other characters that dot notation cannot handle.