disp disp - 13 days ago 5
JSON Question

How to have a string in valid json that contains a URL?

I want to create a valid json object, that contains a URL, e.g.

{"a": "b http:\/\/stackoverflow.com\/questions\/ask c"}


I could:


  • (A) escape all forward slashes in the URL text with backslashes

  • (B) put the URL into
    \\url{}



My questions are:


  1. Are there any other ways to do this? (Note: I do not mean libraries or tools to automate it, but valid ways to encode or escape the URL in json.)

  2. Which possibility is considered best practice?

  3. Are there known issues when parsing URLs encoding like in (B) in the major programming languages that make use of json objects, like Java, Python, etc?


Answer

A quick note that you do not have to escape forward slashes in the official JSON spec. However forward slashes are the only non-special JSON characters that are allowed to be escaped.

Also a quick note that no valid (web) url should contain characters that need to be escaped.

In the slightly broader case of encoding JSON strings that contain carraige returns, tabs, etc (e.g. backslashes must be escaped via \\), best practice is to follow the spec and escape the characters using backslash. The programming languages you mentioned have no problems with backslash-escaped characters.