Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
kyleabaker
Visitor

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?
0 Kudos
6 REPLIES 6
destruk
Binge Watcher

Re: JSON and Regex Escape Characters

You could, before parsing, do a string replace on the data, and send the result to the JSON parser.
0 Kudos
RokuMarkn
Visitor

Re: JSON and Regex Escape Characters

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
0 Kudos
kyleabaker
Visitor

Re: JSON and Regex Escape Characters

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?
0 Kudos
kyleabaker
Visitor

Re: JSON and Regex Escape Characters

"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.
0 Kudos
NewManLiving
Visitor

Re: JSON and Regex Escape Characters

You can try rostring tokenize Solved a few similar problems for me
My Channels: 2D API Framework Presentation: https://owner.roku.com/add/2M9LCVC
Updated: 11-11-2015 - Completed Keyboard interface
The Joel Channel ( Final Beta )
0 Kudos
EnTerr
Roku Guru

Re: JSON and Regex Escape Characters

"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).
0 Kudos