3

I am trying to use a web API of a service written in Perl (OTRS). The data is sent in JSON format.

One of the string values inside the JSON structure contains a pound sign, which in apparently is used as a comment character in JSON. This results in a parsing error:

unexpected end of string while parsing JSON string

I couldn't find how to escape the character in order to get the string parsed successfully. The obvious slash escaping results in:

illegal backslash escape sequence in string

Any ideas how to escape it?

Update: The URL I am trying to use looks something like that (simplified but still causes the error):

http://otrs.server.url/otrs/json.pl?User=username&Password=password&Object=TicketObject&Method=ArticleSend&Data={"Subject":"[Ticket#100000] Test Ticket from OTRS"}
Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
Variant
  • 17,279
  • 4
  • 40
  • 65
  • 3
    when you say 'pound sign', do you mean # or £? Please note that the name 'pound' is not universally recognised for the former. :) – Spudley Apr 07 '11 at 10:08

2 Answers2

7

Use Uri::escape:

use URI::Escape;
my $safe = uri_escape($url);

See rfc1738 for the list of characters which can be unsafe.

Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
5

The hash symbol, #, has a special meaning in URLs, not in JSON. Your URL is probably getting truncated at the hash before the remove server even sees it:

http://otrs.server.url/otrs/json.pl?User=username&Password=password&Object=TicketObject&Method=ArticleSend&Data={"Subject":"[Ticket

And that means that the remote server gets mangled JSON in Data. The solution is to URL encode your parameters before pasting them together to form your URL; eugene y tells you how to do this.

mu is too short
  • 426,620
  • 70
  • 833
  • 800