JSON Support Comes to Roku

With the 4.8 software release for Roku 2, Roku LT and Roku HD players, developers now have access to native JSON parsing support from BrightScript.  Roku channel developers no longer need to integrate third party solutions or worse, roll their own parsing solutions anymore.  Developers can easily consume JSON based feeds or REST API response payloads with one simple API call.

To parse JSON from BrightScript, simply call the new ParseJSON API:

ParseJSON(jsonString As String) As Object

ParseJSON takes a single string argument that contains the JSON content that you want to parse.  This can come from any source, such as a text file installed as part of your channel or the response from an HTTP request executed with an roUrlTransfer object.  If the string contains valid, well-formed JSON, the object returned is an associative array representation of the parsed JSON.

For example, let’s assume that the string jsonAsString contains the following JSON content:

{
 "Videos" : [
  {
   "Title" : "Jeff Han Demos his Breakthrough Touchscreen",
   "Image" : "http://rokudev.roku.com/rokudev/examples/videoplayer/images/JeffHan.jpg",
   "Url" : "http://video.ted.com/talks/podcast/JeffHan_2006_480.mp4"
 },
 {
   "Title" : "David Kelley on Human-Centered Design",
   "Image" : "http://rokudev.roku.com/rokudev/examples/videoplayer/images/DavidKelley.jpg",
   "Url" : "http://video.ted.com/talks/podcast/DavidKelley_2002_480.mp4"
  },
  …
 ]
}

A call to ParseJSON would then return an object that can be used like this:

json = ParseJSON(jsonAsString)
for each video in json.Videos
  print video.Title; video.Image; video.Url
end for

To help get you started using Roku’s new JSON support, I’ve built a short sample application that demonstrates how to parse a JSON text file which populates the content in a simple poster screen style UI.  This application also provides an XML driven version of the same functionality so that you can compare JSON parsing to XML parsing in one convenient sample. The sample can be found here:

Download BrightScript JSON Parsing Sample

More details on the new JSON API can be found in the Roku SDK documentation:
More on the JSON API

About Robert Burdick

Roku Developer Support Manager
This entry was posted in Uncategorized. Bookmark the permalink.
  • http://twitter.com/klittle212 Ken Little
    • Rburdick

      Thanks for this Ken. The link in the article has been corrected.

  • Brenton House

    The only problem with this is that the roAssociativeArray that is returned by ParseJSON is case sensitive and if you pass a case sensitive roAssociativeArray (as Video) to SetContentList(), then then nothing will work when you call Show(). You can recreate this by simply calling SetModeCaseSensitive() on your existing Video roAssociativeArrays before calling SetContentList().

  • Pingback: Communicating with Web Services from BrightScript | Developer Blog

  • http://wlaurance.com/ Will Laurance

    Sweet! Writing a local node.js media server and I really didn’t want to use XML :) :)

  • brentonhouse

    Any chance the issue with SetContentList() and the case-sensative array created by JSON has been fixed with the 5.0 SDK? Please say yes! Please say yes!

  • tmv105

    I’m struggling to get the sample to work with both json and xml. Here is the response I get from the json choice:

    Current Function:

    057: Function LoadJSONFile() as void

    058: jsonAsString = ReadAsciiFile(“pkg:/json/sample1.json”)

    059: m.json = ParseJSON(jsonAsString)

    060: ShowPosterScreen(m.json.Videos, true)

    061: End Function

    ‘Dot’ Operator attempted with invalid BrightScript Component or interface reference. (runtime error &hec) in …AALAE4Bk/pkg:/source/main.brs(60)

    060: ShowPosterScreen(m.json.Videos, true)

    Backtrace:

    Function loadjsonfile() As

    file/line: /tmp/plugin/D…AALAE4Bk/pkg:/source/main.brs(60)

    Function handlebuttonpress(id As Integer) As

    file/line: /tmp/plugin/D…AALAE4Bk/pkg:/source/main.brs(51)

    Function main() As

    file/line: /tmp/plugin/D…AALAE4Bk/pkg:/source/main.brs(26)

    Local Variables:

    global &h0020 rotINTERFACE:ifGlobal

    m &h0010 bsc:roAssociativeArray, refcnt=4

    jsonasstring &h8010 bsc:roString (2.1 was String), refcnt=1

    BrightScript Debugger>

    I’m new to all this and trying to figure out the debugger and what the msgs mean. Any help?