Search code examples
jsonpointer

Why does json pointer use tilde as the escape character?


The JSONPointer notation (rfc6901) allows you to denote a location in a JSON document as a string.

I was surprised to see that the specification uses a tilde '~' as the escape character? Why was this chosen rather than something more conventioal like a backslash ''?


Solution

  • The reason backslash cannot be used is that backslash already has a meaning in JSON and it is desirable to be able to include a JSONPointer in a JSON document without having to double escape it.

    If you read the specification carefully you will note:

    1. JSON String Representation

      A JSON Pointer can be represented in a JSON string value. Per
      [RFC4627], Section 2.5, all instances of quotation mark '"' (%x22),
      reverse solidus '\' (%x5C), and control (%x00-1F) characters MUST be
      escaped.

      Note that before processing a JSON string as a JSON Pointer,
      backslash escape sequences must be unescaped.

    Another reason is to allow for URI encoding.

    According to this discussion it was almost caret '^' instead. Note also that tilde '~' is allowed in URLs whereas caret '^' is not. Though see http://jkorpela.fi/tilde.html for a counterpoint to tildes in URLs.