Forum Discussion

kyleabaker's avatar
kyleabaker
Visitor
12 years ago

JSON and Regex Escape Characters

I'm parsing a JSON response and am finding that some year strings are returned with the following format in JSON:

...,"Year":"2009\u2013",...

When I set year = Content.Year I'd like to replace the \u with a dash (ex. 2009-2013), but its showing up as "2009-" currently and I'm not able to even see the second year. I see that \u appears to be an escape character. Any idea how I can get this date range formatted correctly?

6 Replies

  • destruk's avatar
    destruk
    Streaming Star
    You could, before parsing, do a string replace on the data, and send the result to the JSON parser.
  • You just want to replace the string "\u" with "-", right? It would help if you showed what code you've tried, but this will work. Note that since backslash is a regex metacharacter, you have to escape it.


    regex = CreateObject("roRegex", "\\u", "")
    year = regex.Replace(year, "-")


    --Mark
  • Mark,

    That's the exact regex that I was trying as well, however, its not doing what we're expecting here. Below is what I see in the debug console when I print Content.Year. Then I execute this regex and print that below. Doesn't seem to be replacing anything and you can see the Ç char is printed for some reason.

    2009                                  Ç
    2009 Ç


    Is the "\u2013" being displayed as this Ç character?
  • "destruk" wrote:
    You could, before parsing, do a string replace on the data, and send the result to the JSON parser.


    That's a possible solution, but seems like I should be able to handle this data without processing it before JSON parsing. If all else fails I'll give that a shot.
  • "kyleabaker" wrote:
    I'm parsing a JSON response and am finding that some year strings are returned with the following format in JSON:
    ...,"Year":"2009\u2013",...
    When I set year = Content.Year I'd like to replace the \u with a dash (ex. 2009-2013), but its showing up as "2009-" currently and I'm not able to even see the second year. I see that \u appears to be an escape character. Any idea how I can get this date range formatted correctly?

    You guys, beating the wrong horse here.
    There is no year "2013" in the string - rather \u2013 is a single character, EN DASH. Same as chr(&h2013) and chr(8211) if you want it in B/S. Due to looking as a year number, confusion is understandable. Apropos, \u2014 is EM DASH (dash with the width of letter "M").

    It should be displayed as "2009-"; i suppose it means "2009 and later".

    "kyleabaker" wrote:
    Doesn't seem to be replacing anything and you can see the Ç char is printed for some reason.
    Yes, it is not replacing anything, because there is no r"\u" in the string. The display of EN DASH as tabs+Ç is some idiosyncrasy of the console, which i rather not dig into (could be the telnet protocol, terminal emulator etc).