JMESPath: escape characters in literals
How JMESPath escapes characters in raw string literals, JSON literals and quoted identifiers, and where shells and config files add extra escaping layers.
JMESPath has three quoting forms, and each one handles escape characters differently. Most escaping problems come from mixing them up, or from forgetting that a shell, configuration file or programming language adds its own escaping before JMESPath ever sees the expression. This review covers what each form does with a backslash, what escaping actually changes, and how well an escaped expression survives being copied between tools.
Three quoting forms, three sets of escape rules
Single quotes create a raw string literal, which is a string value taken almost exactly as written. Backticks wrap a JSON literal, which can be any JSON value, including numbers, booleans, null, arrays and objects. Double quotes create a quoted identifier, which is a field name rather than a string value. The escapes each form recognizes follow from what it is for.
| Syntax | Produces | Delimiter to escape | Escapes recognized |
| '...' raw string literal | A string value | Single quote, written ' | ' for a single quote. The current specification grammar also treats \ as one backslash. Any other backslash is kept as written. |
| ... JSON literal | Any JSON value | Backtick, written ` | ` for a backtick, then the standard JSON escapes inside the JSON text, such as ", \, \n, \t and \uXXXX |
| "..." quoted identifier | A field name | Double quote, written " | The standard JSON string escapes, such as ", \, \n, \t and \uXXXX |
What escaping changes, and what it does not
Escaping changes only the source text of the expression. It lets a delimiter or special character appear inside a literal without ending it early. The value JMESPath compares or returns contains the intended character, not the backslash. Nothing is reversed, mirrored, restyled or substituted with look-alike Unicode characters. If the escaping is correct, the stored data and the evaluated value are unchanged. The examples below assume the input document shown and current-specification behavior. Results are written as JSON, so ["a\b"] means a string containing a single backslash.
Input document:
{"first name": "Ana", "my-key": 1, "tags": ["it's", "a\\b", "café"]}
Expression Result
"first name" "Ana"
"my-key" 1
tags[?@ == 'it\'s'] ["it's"]
tags[?@ == `"it's"`] ["it's"]
tags[?@ == `"a\\b"`] ["a\\b"]
tags[?@ == `"caf\u00e9"`] ["café"]
tags[?@ == "it's"] []
The last line shows the most common trap. Double quotes do not make a string in JMESPath. "it's" is read as a field named it's on each element. A string element has no fields, so the lookup yields null, the comparison fails and the filter returns an empty array without raising an error. Use quoted identifiers for keys that contain spaces, hyphens, leading digits or other characters an unquoted identifier cannot hold, and use single quotes or backticks for values.
Backslashes and Unicode: where implementations may differ
Raw string literals deliberately do very little escape processing. Under the current grammar, 'a\b' produces a\b. Earlier specification text and some implementations, however, recognized only ' inside raw strings and left every other backslash as written, so the same expression could produce a\b and fail to match. This review has no test results across libraries. If a raw string must contain a backslash, test it against the library and version you actually use, or switch to a JSON literal such as "a\\b", whose JSON escape rules are unambiguous.
Raw strings also do not decode Unicode escapes. 'caf\u00e9' contains a literal backslash followed by u00e9, not the character é. Use the character itself inside the raw string, or use a JSON literal such as "caf\u00e9", when you need \u escapes or control characters like newlines and tabs.
Some implementations also accept a backtick literal without inner JSON quotes, such as foo, and treat it as the string foo for backward compatibility. The specification deprecates this form. Write "foo" or 'foo' instead of relying on it.
Copying expressions between shells, files and documents
Every host that carries a JMESPath expression adds its own escaping layer, and that layer is applied first. On the command line, the shell decides which quotes and backslashes reach JMESPath. In bash, backticks inside a double-quoted argument trigger command substitution, so they must be escaped or the whole argument must be single-quoted. The examples below use the AWS CLI --query option as an illustration of bash quoting only. PowerShell and the Windows command prompt follow different rules that are not covered here.
# Double-quoted shell argument: JMESPath single-quoted raw strings pass through unchanged
aws ec2 describe-instances --query "Reservations[].Instances[?State.Name == 'running'].InstanceId"
# Backticks inside double quotes are command substitution in bash, so escape them
aws ec2 describe-instances --query "Reservations[].Instances[?State.Code == \`16\`].InstanceId"
# Or single-quote the whole argument so backticks reach JMESPath untouched
aws ec2 describe-instances --query 'Reservations[].Instances[?State.Code == `16`].InstanceId'
The same stacking happens when an expression is stored in a JSON configuration file or a programming-language string. In JSON, each backslash in the expression must be written as two, so a JMESPath JSON literal that matches one backslash, "a\\b", needs four backslashes in the file. Where the host language offers raw strings, such as Python's r'...' form, they remove one layer. Keeping complex expressions in their own file is another way to avoid nested escaping.
Copying from word processors, chat tools or rich-text documentation can break an expression silently. Curly quotes and look-alike accent marks are not JMESPath delimiters, so a pasted expression may fail to parse or may match differently. Share expressions in code formatting or plain-text files. When an expression fails after pasting, retype the quote and backtick characters before looking for other causes.
Heavily escaped expressions are hard to scan by eye and awkward when read aloud character by character. No assistive-technology testing was available for this review. When sharing an expression with people rather than with a program, add a plain-language description of what it matches, for example "tags equal to the string it's". That gives readers a plain-text equivalent that does not depend on reading backslashes and backticks correctly.
Which form fits your use
JMESPath escaping is suitable for matching exact string values and awkward key names, provided the right quoting form is used for the job. The main benefit is precision: the evaluated value contains exactly the intended characters. The decisive tradeoff is layering. Each host format adds escapes, readability drops quickly, and raw-string backslash handling has not been consistent across implementations.
- Use a raw string literal ('...') for ordinary text values, escaping only single quotes as '.
- Use a JSON literal (...) for numbers, booleans, null, arrays, objects, \u escapes, control characters or any value containing a backslash.
- Use a quoted identifier ("...") only for field names that unquoted identifiers cannot express.
- Prefer plain unescaped text when the value has no delimiters or backslashes. Simple literals travel more reliably.
- When escaping stacks across three or more layers, consider doing the comparison in the host language, or moving the expression into its own file.
What this review could not confirm
No subject-specific test evidence was supplied. The examples follow the published grammar but were not run against named libraries or versions, so how each implementation handles backslashes in raw strings is unverified. Shell guidance covers bash only. Screen reader and other assistive-technology behavior with escaped expressions has not been tested.