Skip to content

Instantly share code, notes, and snippets.

@timbeynart-nbcuni
Last active January 11, 2023 17:38
Show Gist options
  • Save timbeynart-nbcuni/c73989ff347b101ef4176b8c104f82af to your computer and use it in GitHub Desktop.
Save timbeynart-nbcuni/c73989ff347b101ef4176b8c104f82af to your computer and use it in GitHub Desktop.
Code examples for parsing a JSON feed into a RowList

This example shows how to load a JSON feed into a ScenGraph RowList in Roku. It's not a complete application. This code started life as Simple_Grid_with_Details_and_Video, available here: https://blog.roku.com/developer/2016/03/03/scenegraph-tutorial/

The json_parser.brs file has the brightscript code to load the feed data (represented by the contents of data.json). The data is then translated into an array of MediaItems. MediaItem is an extended ContentNode. Then the data is structured to load into a RowList, which is represented in GridScreen.xml.

{
"$xmlns": {
"pl1": "http://access.auth.theplatform.com/data/Account/2295898951",
"nbcu": "http://xml.nbcuni.com/media/field"
},
"startIndex": 1,
"itemsPerPage": 3,
"entryCount": 3,
"title": "PROD Feed for Video Assets",
"description": "PROD Feed for Video Assets",
"entries": [
{
"id": "http://data.media.theplatform.com/media/data/Media/939146307771",
"guid": "3515752",
"updated": 1494453617000,
"title": "Queen of the South - There’s A New Queen In Town",
"description": "Queen of the South premieres Thursday, June 8 at 10/9c.",
"added": 1494285069000,
"availableDate": 1494453600000,
"expirationDate": 1497002400000,
"categories": [
{
"name": "Series/Queen of the South",
"scheme": "",
"label": ""
}
],
"copyright": "",
"copyrightUrl": "",
"keywords": "Queen of the South,Alice Braga,Adrana Barraza,Hemky Madera,James Martinez,Joaquim de Almeida",
"ratings": [
{
"scheme": "urn:mpaa",
"rating": "nr",
"subRatings": [
]
}
],
"countries": [
"GU",
"MP",
"PR",
"US",
"VI"
],
"excludeCountries": false,
"content": [
{
"duration": 30.030,
"format": "MPEG4",
"height": 240,
"isDefault": false,
"language": "en",
"url": "http://link.theplatform.com/...",
"width": 416
},
{
"duration": 30.030,
"format": "MPEG4",
"height": 544,
"isDefault": false,
"language": "en",
"url": "http://link.theplatform.com/...",
"width": 960
},
{
"duration": 30.000,
"format": "MPEG4",
"height": 720,
"isDefault": false,
"language": "en",
"url": "http://link.theplatform.com/...",
"width": 1280
},
{
"duration": 30.000,
"format": "MPEG4",
"height": 540,
"isDefault": false,
"language": "en",
"url": "http://link.theplatform.com/...",
"width": 960
},
{
"duration": 30.000,
"format": "MPEG4",
"height": 1080,
"isDefault": true,
"language": "en",
"url": "http://link.theplatform.com/...",
"width": 1920
},
{
"duration": 30.000,
"format": "MPEG4",
"height": 360,
"isDefault": false,
"language": "en",
"url": "http://link.theplatform.com/...",
"width": 640
},
{
"duration": 30.000,
"format": "MPEG4",
"height": 234,
"isDefault": false,
"language": "en",
"url": "http://link.theplatform.com/...",
"width": 416
},
{
"duration": 0.000,
"format": "TCM",
"height": 0,
"isDefault": false,
"language": "",
"url": "http://link.theplatform.com/...",
"width": 0
}
],
"thumbnails": [
{
"duration": 0.000,
"format": "JPEG",
"height": 1080,
"isDefault": false,
"language": "",
"url": "http://tve-static-usanetwork.nbcuni.com/prod/image/664/731/170508_3515752_Queen_of_the_South___Action_Packed_anvver_2.jpg",
"width": 1920
}
],
"pubDate": 1494283080000,
"defaultThumbnailUrl": "http://tve-static-usanetwork.nbcuni.com/prod/image/664/731/170508_3515752_Queen_of_the_South___Action_Packed_anvver_2_800x450_939405379534.jpg",
"provider": "USA",
"pl1$dayPart": "Primetime",
"pl1$entitlement": "free",
"pl1$event": "",
"pl1$externalAdvertiserId": "USA_ANV_3515752",
"pl1$fullEpisode": false,
"pl1$promo": false,
"pl1$seasonNumber": 2,
"pl1$shortDescription": "Queen of the South premieres Thursday, June 8 at 10/9c.",
"pl1$shortTitle": "Queen of the South - There’s A New Queen In Town",
"nbcu$advertisingGenre": "Drama",
"nbcu$programmingType": "Undefined"
},
{
"id": "http://data.media.theplatform.com/media/data/Media/939128899982",
"guid": "3515747",
"updated": 1494324030000,
"title": "Queen of the South - All The Way Up",
"description": "Catch the premiere of Queen of the South Thursday, June 8 at 10/9c.",
"added": 1494283878000,
"availableDate": 1494324000000,
"expirationDate": 1497002400000,
"categories": [
{
"name": "Series/Queen of the South",
"scheme": "",
"label": ""
}
],
"copyright": "",
"copyrightUrl": "",
"keywords": "Queen of the South,Alice Braga,Adrana Barraza,Hemky Madera,James Martinez,Joaquim de Almeida",
"ratings": [
{
"scheme": "urn:mpaa",
"rating": "nr",
"subRatings": [
]
}
],
"countries": [
"GU",
"MP",
"PR",
"US",
"VI"
],
"excludeCountries": false,
"content": [
{
"duration": 60.000,
"format": "MPEG4",
"height": 360,
"isDefault": false,
"language": "en",
"url": "http://link.theplatform.com/...",
"width": 640
},
{
"duration": 60.061,
"format": "MPEG4",
"height": 544,
"isDefault": false,
"language": "en",
"url": "http://link.theplatform.com/...",
"width": 960
},
{
"duration": 60.000,
"format": "MPEG4",
"height": 234,
"isDefault": false,
"language": "en",
"url": "http://link.theplatform.com/...",
"width": 416
},
{
"duration": 60.061,
"format": "MPEG4",
"height": 240,
"isDefault": false,
"language": "en",
"url": "http://link.theplatform.com/...",
"width": 416
},
{
"duration": 60.000,
"format": "MPEG4",
"height": 720,
"isDefault": false,
"language": "en",
"url": "http://link.theplatform.com/...",
"width": 1280
},
{
"duration": 60.000,
"format": "MPEG4",
"height": 540,
"isDefault": false,
"language": "en",
"url": "http://link.theplatform.com/...",
"width": 960
},
{
"duration": 60.000,
"format": "MPEG4",
"height": 1080,
"isDefault": true,
"language": "en",
"url": "http://link.theplatform.com/...",
"width": 1920
},
{
"duration": 0.000,
"format": "TCM",
"height": 0,
"isDefault": false,
"language": "",
"url": "http://link.theplatform.com/...",
"width": 0
}
],
"thumbnails": [
{
"duration": 0.000,
"format": "JPEG",
"height": 1080,
"isDefault": false,
"language": "",
"url": "http://tve-static-usanetwork.nbcuni.com/prod/image/648/115/170508_3515747_Queen_of_the_South___All_The_Way_Up.jpg",
"width": 1920
}
],
"pubDate": 1494282540000,
"defaultThumbnailUrl": "http://tve-static-usanetwork.nbcuni.com/prod/image/648/115/170508_3515747_Queen_of_the_South___All_The_Way_Up_800x450_939133507784.jpg",
"provider": "USA",
"pl1$dayPart": "Primetime",
"pl1$entitlement": "free",
"pl1$event": "",
"pl1$externalAdvertiserId": "USA_ANV_3515747",
"pl1$fullEpisode": false,
"pl1$promo": false,
"pl1$seasonNumber": 2,
"pl1$shortDescription": "Catch the premiere of Queen of the South Thursday, June 8 at 10/9c.",
"pl1$shortTitle": "Queen of the South - All The Way Up",
"nbcu$advertisingGenre": "Drama",
"nbcu$programmingType": "Undefined"
},
{
"id": "http://data.media.theplatform.com/media/data/Media/914045507807",
"guid": "3496379",
"updated": 1494151208000,
"title": "Big Star Little Star Interview: Penn Jillette, Brooke Burke, Harold Perrineau",
"description": "Meet the celebrity contestants of the hilarious new game show, BIG STAR LITTLE STAR. Check out more exclusive BIG STAR LITTLE STAR videos, galleries and more at usanetwork.com.",
"added": 1491347776000,
"availableDate": 1494151200000,
"expirationDate": 1499853600000,
"categories": [
{
"name": "Series/Big Star Little Star",
"scheme": "",
"label": ""
}
],
"copyright": "",
"copyrightUrl": "",
"keywords": "Big Star Little Star,Cat Deeley,Corin Nelson,David George,Brian Zagorsk",
"ratings": [
{
"scheme": "urn:v-chip",
"rating": "tv-pg",
"subRatings": [
]
}
],
"countries": [
],
"excludeCountries": true,
"content": [
{
"duration": 63.000,
"format": "MPEG4",
"height": 1080,
"isDefault": true,
"language": "en",
"url": "http://link.theplatform.com/...",
"width": 1920
},
{
"duration": 63.000,
"format": "MPEG4",
"height": 720,
"isDefault": false,
"language": "en",
"url": "http://link.theplatform.com/...",
"width": 1280
},
{
"duration": 63.000,
"format": "MPEG4",
"height": 360,
"isDefault": false,
"language": "en",
"url": "http://link.theplatform.com/...",
"width": 640
},
{
"duration": 63.364,
"format": "MPEG4",
"height": 240,
"isDefault": false,
"language": "en",
"url": "http://link.theplatform.com/...",
"width": 416
},
{
"duration": 63.000,
"format": "MPEG4",
"height": 540,
"isDefault": false,
"language": "en",
"url": "http://link.theplatform.com/...",
"width": 960
},
{
"duration": 0.000,
"format": "TCM",
"height": 0,
"isDefault": false,
"language": "",
"url": "http://link.theplatform.com/...",
"width": 0
},
{
"duration": 63.000,
"format": "MPEG4",
"height": 234,
"isDefault": false,
"language": "en",
"url": "http://link.theplatform.com/...",
"width": 416
},
{
"duration": 63.364,
"format": "MPEG4",
"height": 544,
"isDefault": false,
"language": "en",
"url": "http://link.theplatform.com/...",
"width": 960
}
],
"thumbnails": [
{
"duration": 0.000,
"format": "JPEG",
"height": 1080,
"isDefault": false,
"language": "",
"url": "http://tve-static-usanetwork.nbcuni.com/prod/image/278/743/170404_3496379_Big_Star_Little_Star_Interview___Penn_Jillet.jpg",
"width": 1920
}
],
"pubDate": 1491341820000,
"defaultThumbnailUrl": "http://tve-static-usanetwork.nbcuni.com/prod/image/278/743/170404_3496379_Big_Star_Little_Star_Interview___Penn_Jillet_800x450_914051139503.jpg",
"provider": "USA",
"pl1$dayPart": "Primetime",
"pl1$entitlement": "free",
"pl1$event": "",
"pl1$externalAdvertiserId": "USA_ANV_3496379",
"pl1$fullEpisode": false,
"pl1$promo": false,
"pl1$seasonNumber": 1,
"pl1$shortDescription": "Meet the celebrity contestants of the hilarious new game show, BIG STAR LITTLE STAR. Check out more exclusive BIG STAR LITTLE STAR videos, galleries and more at usanetwork.com.",
"pl1$shortTitle": "Big Star Little Star Interview: Penn Jillette, Brooke Burke, Harold Perrineau",
"nbcu$advertisingGenre": "Reality and Game Show",
"nbcu$programmingType": "Behind the Scenes"
}
]
}
<?xml version="1.0" encoding="utf-8" ?>
<component name="GridScreen"
extends="Group"
initialFocus="RowList">
<!-- main handler -->
<script type="text/brightscript"
uri="pkg:/components/screens/GridScreen.brs" />
<interface>
<!-- Grid Content Node -->
<field id="content"
type="node"
alias="RowList.content"
onChange="onContentChange" />
<!-- Row item selection handler -->
<field id="rowItemSelected"
type="intarray"
alwaysnotify="true"
alias="RowList.rowItemSelected" />
<!-- Row item focused handler -->
<field id="itemFocused"
type="intarray"
alias="RowList.rowItemFocused"
onChange="OnItemFocused" />
<!-- Interface to focused item (Content Node) -->
<field id="focusedContent"
type="node" />
</interface>
<children>
<RowList id="RowList"
translation="[200, 300]"
itemSize="[1920, 225]"
numRows="3"
itemSpacing="[0, 100]"
rowFocusAnimationStyle="fixedFocusWrap"
rowItemSize="[[400, 225]]"
rowItemSpacing="[[20, 0]]"
showRowLabel="true"
showRowCounter="false"
rowLabelOffset="[[20, 20]]"
loadingBitmapOpacity="1.0"
failedBitmapOpacity="1.0"
loadingBitmapUri="pkg:/images/gmo_logo_hd.png"
failedBitmapUri="pkg:/images/gmo_logo_hd.png" />
</children>
</component>
<?xml version="1.0" encoding="utf-8" ?>
<component name="MediaItem" extends="ContentNode">
<interface>
<field id="thumbnail" type="string" />
<field id="contentType" type="string" />
<field id="title" type="string" />
<field id="entitlement" type="string" />
<field id="externalAdvertiserId" type="string" />
<field id="mpxMediaGuid" type="string" />
<field id="mpxAccountId" type="string" />
<field id="duration" type="float" />
<field id="trackingData" type="assocarray" />
<field id="category" type="string" />
<field id="stream" type="assocarray" />
</interface>
</component>
sub loadContent()
shortFormRow = GetShortForm()
list = [
{
TITLE : "Short Form"
ContentList : shortFormRow
}
]
m.contentselection = m.top.findNode("GridScreen")
m.contentselection.content = ParseContent(list)
end sub
Function GetShortForm()
url = CreateObject("roUrlTransfer")
url.SetUrl("http://yourcontentprovider.com/data.json")
rsp = url.GetToString()
response = parseJSON(rsp)
result = parseFeed(response)
return result
End Function
function parseFeed(feed)
result = []
regex = CreateObject("roRegex", "/", "i")
for each item in feed.entries
asset = {}
asset.HDPosterUrl = item.defaultThumbnailUrl
asset.hdBackgroundImageUrl = item.defaultThumbnailUrl
asset.streamFormat = "hls"
asset.Categories=[]
asset.filmstrip= item.filmstrip
asset.thumbnail = item.defaultThumbnailUrl
asset.title = item.title
asset.description = item.description
asset.releasedate = item.added
asset.mpxmediaguid = item.guid
asset.entitlement = item["pl1$entitlement"]
asset.externaladvertiserid =item["pl1$externalAdvertiserId"]
asset.duration = item.content[0].duration
if item.ownerId <> invalid
asset.mpxAccountId = regex.Split(item.ownerId).peek()
else
asset.mpxAccountId = "2304992029"
end if
if item.categories <> invalid AND item.categories[0] <> invalid
asset.category=item.categories[0].name
end if
mediaType = "VOD Clip"
if item["pl1$fullEpisode"] = true OR item["nbcu$fullEpisode"] = true
mediaType = "VOD Episode"
end if
asset.trackingData = {}
asset.trackingdata["tve.videoprogram"] = item.title
asset.trackingdata["tve.videocontent"] = mediaType
asset.TrackingData["tve.length"] = item.content[0].duration
asset.TrackingData["tve.season"] = "999"
asset.TrackingData["tve.epnumber"] = "888"
asset.TrackingData["tve.airdate"] = "2001-04-01"
asset.stream = {url:""}
result.push(asset)
end for
return result
end function
Function ParseContent(list As Object)
RowItems = createObject("RoSGNode","ContentNode")
for each rowAA in list
row = createObject("RoSGNode","ContentNode")
row.Title = rowAA.Title
for each itemAA in rowAA.ContentList
item = createObject("RoSGNode","MediaItem")
' We don't use item.setFields(itemAA) as doesn't cast streamFormat to proper value
for each key in itemAA
item[key] = itemAA[key]
end for
row.appendChild(item)
end for
RowItems.appendChild(row)
end for
return RowItems
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment