-1

I am using JSON.stringify to put an array into the search part of the URL. By default JSON uses square brackets, [] for arrays.

However, square brackets in URLs sometimes lead to strange problems. For example when sending a URL with square brackets in an email with Thunderbird the hyperlink gets cut off just before the first occurrence of a square bracket.

Is it possible to make JSON use another style of brackets? E.g. normal brackets, ()?

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Daniel
  • 3,383
  • 4
  • 30
  • 61
  • 5
    What you have there is an [XY problem](http://meta.stackexchange.com/a/66378/169187). See http://stackoverflow.com/questions/332872/how-to-encode-a-url-in-javascript and http://stackoverflow.com/questions/75980/best-practice-escape-or-encodeuri-encodeuricomponent – JJJ Jul 04 '14 at 10:23
  • Thanks for your comment. Unfortunately, both `encodeURI` and `encodeURIComponent` leave square brackets untouched (tested in Firefox). – Daniel Jul 04 '14 at 10:55
  • 1
    It still seems you are not encoding your data properly or don't process it correctly on the server. Square brackets work usually very well in URLs. – Felix Kling Jul 04 '14 at 10:58
  • Oh, my mistake, I forgot adding that the problem with sending of the URL is in a Thunderbird mail. No problem in my browser with square brackets. I just want to make it save for other programs, like Thunderbird, as well. – Daniel Jul 04 '14 at 11:02
  • 1
    You can't tell JSON to use another type of brackets, because then *it's not JSON*. You can, however, replace the square brackets yourself after generating the string - more than enough information on how to do that available already. Just make sure to do the opposite before you try to parse the string as JSON. – Anthony Grist Jul 04 '14 at 11:08
  • Thanks for the comment. Not sure where this kind of information is available, e.g. an easy `.replace(/\[/g, "(").replace(/\]/g, ")")` will not do, since there may be square brackets in the values of the array which are lost when decoding again with `.replace`. That's why I was wondering whether there is a specific (related) JSON function for it. – Daniel Jul 04 '14 at 11:20

1 Answers1

0

No, JSON doesnt offer this functionality, what you would have to do is replace the [] with something unique before you transfer your data, then replace them back on the server, but realistically its a bad idea to pass json in a url via get

iConnor
  • 19,997
  • 14
  • 62
  • 97
  • Thanks, I may try to work around it as you suggested. Not sure why it is in general a bad idea to pass JSON in a URL. What would be the alternative? Passing in a URL has some nice benefits, like save data via bookmarking. – Daniel Jul 05 '14 at 12:44