parse

fun parse(expression: String): VariantPath

Parses an expression in the subset of JSONPath the engine uses:

$                    the document
$.user.name a field of a field
$.items[0] an array element
$["odd name"] a field whose name is not a bare identifier

Deliberately not full JSONPath. Wildcards, slices and filters are query concerns; a path here identifies exactly one location, which is what makes it usable as an index key.

A field name that is not [A-Za-z0-9_]+ requires the bracket form, and $["odd name"] above understates how ordinary that is. $.@type does not parse — the dot form takes an identifier — so in a protobuf-JSON corpus, where @type is on every message, the bracket form is not an edge case but the rule. In Kotlin the readable spelling is a raw string: VariantPath.parse("""$["@type"]""").

Inside the quotes a backslash escapes the next character literally, so $["a\nb"] is the three-character name anb and not a, newline, b; a real newline in a name is written raw and round-trips. Self-consistent, and deliberately not RFC 9535 §2.7's escaping — toNormalizedPath and parseNormalized are that grammar, and they are the pair to reach for when the path is going somewhere outside the engine.

Throws

with the offending position, for malformed input.