Roku Developer Program

Developers and content creators—a complete solution for growing an audience directly.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Highlighted
Level 7

Can brightscript parse json?

Dear all,

I have been looking in the docs and I believe the answer to my question is NO but I wanted to make sure...
Can brightscript parse json? (natively, without having to resort to regex)?

Thank you,
Luis
0 Kudos
8 Replies
Highlighted
Roku Employee
Roku Employee

Re: Can brightscript parse json?

The answer is no. There has been a lot of discussion on this topic in the past. Often times, a quick search of the forum will get you an answer faster than starting a new thread...

search.php?st=0&sk=t&sd=d&sr=posts&keywords=json&fid%5B%5D=34
0 Kudos
Highlighted
Level 7

Re: Can brightscript parse json?

Thank you, my bad, you are right, I should have done a search on the forum but I only focused on the docs...

I haven't been having a lot of luck finding stuff on the forum so that's why I didn't even bother to search on it - I apologize for wasting your time!
0 Kudos
Highlighted
Level 7

Re: Can brightscript parse json?

Ther eis no JSON parser in the core BrightScript language, but depending on the size of the JSON, and whether it needs to be completely reversible, there are libraries to help: https://github.com/rokudev/librokudev/tree/master/source/librokudev/source/rokudev_src.

A few things to note:
It's slow, particularly so with large chunks of JSON.
Key names with spaces will be changed to have underscores, as BrightScript doesn't allow spaces in associative array key names.

That said, you may find it sufficient for your needs.
-- GandK Labs
Check out Reversi! in the channel store!
0 Kudos
Highlighted
Roku Employee
Roku Employee

Re: Can brightscript parse json?

"kbenson" wrote:
as BrightScript doesn't allow spaces in associative array key names.


Actually, I think it does:


BrightScript Debugger> test={}
BrightScript Debugger> test["this is a test"]="hello"
BrightScript Debugger> ?test
this is a test: hello

BrightScript Debugger> ?test["this is a test"]
hello
BrightScript Debugger>


- Joel
0 Kudos
Highlighted
Level 17

Re: Can brightscript parse json?

"kbenson" wrote:
Key names with spaces will be changed to have underscores, as BrightScript doesn't allow spaces in associative array key names.

That's not true. You can have key names with spaces, but you have to use AddReplace and Lookup to access the key. Example:
x = CreateObject("roAssociativeArray")
x.AddReplace("this key has spaces","this is the value")
print x.Lookup("this key has spaces")

-JT
0 Kudos
Highlighted
Level 9

Re: Can brightscript parse json?

"renojim" wrote:
"kbenson" wrote:
Key names with spaces will be changed to have underscores, as BrightScript doesn't allow spaces in associative array key names.

That's not true. You can have key names with spaces, but you have to use AddReplace and Lookup to access the key. Example:
x = CreateObject("roAssociativeArray")
x.AddReplace("this key has spaces","this is the value")
print x.Lookup("this key has spaces")

-JT

Actually, keys with spaces work just fine with the square bracket notation, too...
Example:
x = {}
x["this key has spaces"] = "this is the value"
print x["this key has spaces"]

For clarification for the OP, the problem kbenson was referring to was that the inline associative array syntax that the SimpleJSON parsers use doesn't allow for quoted key names, so spaces have to be replaced. The original JSON parser for BrightScript used a brute force method that did support spaces, but was extremely slow parsing. The SimpleJSON library uses RegEx to finesse the JSON into a format that is natively parsable by BrightScript, with a few tradeoffs.

If the API your using provides an XML format, the native BrightScript XML parser is infinitely faster than the community provided JSON parsers, so that would likely be a better option.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
Highlighted
Level 7

Re: Can brightscript parse json?

Thank you so much!
I tried the librokudev library, pretty cool stuff!

Thanks,
Luis
0 Kudos
Highlighted
Level 7

Re: Can brightscript parse json?

"TheEndless" wrote:
kbenson was referring to was that the inline associative array syntax that the SimpleJSON parsers use doesn't allow for quoted key names, so spaces have to be replaced. The original JSON parser for BrightScript used a brute force method that did support spaces, but was extremely slow parsing. The SimpleJSON library uses RegEx to finesse the JSON into a format that is natively parsable by BrightScript, with a few tradeoffs.


Yep, that's it. I was mis-remembering the details of the problem, and thus misspoke. Thanks for the clarification.
-- GandK Labs
Check out Reversi! in the channel store!
0 Kudos