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
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
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
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!
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.
“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>
“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
“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.
Thank you so much!
I tried the librokudev library, pretty cool stuff!
Thanks,
Luis
“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.