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: 

Component Task to Consume Json

I make a task to consume a json
    m.readerTask = createObject("roSGNode", "ContentReader")
    m.readerTask.setField("uri", "https://.../metadata/x?limit=1")
    m.readerTask.observeField("content", "buildModel")
    m.readerTask.control = "RUN" 

I call the component that extends of task and I put the following:
  content = createObject("roSGNode", "ContentNode")
    readInternet = createObject("roUrlTransfer")
    readInternet.setUrl(m.top.uri)
    json = ParseJSON(readInternet.GetString())
             
I get the following error:
 
051:      readInternet = createObject("roUrlTransfer")
052:*     json = ParseJSON(readInternet.GetString())
Member function not found in BrightScript Component or interface. (runtime error &hf4) in pkg:/components/Content/conten
t.brs(52)
052:     json = ParseJSON(readInternet.GetString())
0 Kudos
6 REPLIES 6
EnTerr
Roku Guru

Re: Component Task to Consume Json

That's because roUrlTransfer has no function named "GetString".
See https://sdkdocs.roku.com/display/sdkdoc ... er+Content for sample code - the pink at the bottom.
0 Kudos

Re: Component Task to Consume Json

Thank you very much but now this:
054:      json = ParseJSON(result)
055:
056:                      print json
057:*                     for each kind in json.editorials
---------------------------------------------
'Dot' Operator attempted with invalid BrightScript Component or interface refere
nce. (runtime error &hec) in pkg:/components/Content/content.brs(57)
057:                     for each kind in json.editorials


This function probe with an interface example the basic navigation that does not use components, but in this new application that use components arise this problem, please can you help me[/size][/color]
0 Kudos
EnTerr
Roku Guru

Re: Component Task to Consume Json

By the error sounds as if `json` is `invalid`. What did the previous line show? Add maybe also a `print type(json)`
056:                      print json
0 Kudos

Re: Component Task to Consume Json

Invalid Return
BRIGHTSCRIPT: ERROR: ParseJSON: Data is empty:

In the basic navigation project if you brought me information, so I coded it and if it returns me:
port = CreateObject("roMessagePort")
    request = CreateObject("roUrlTransfer")
    
    request.AddHeader("Authorization", "Basic <BASE 64 ENCODED SEGMENT WRITE KEY>")
    request.AddHeader("Accept", "application/json")
    request.AddHeader("Content-Type", "application/json")
    request.EnablePeerVerification(false)
    request.EnableHostVerification(false)
    request.RetainBodyOnError(true)
    request.SetCertificatesFile("common:/certs/ca-bundle.crt")
    request.InitClientCertificates()
    request.SetMessagePort(port)
    request.SetUrl("https://.../metadata/x?limit=1")
if (request.AsyncGetToString())
        while (true)
            msg = wait(0, port)
            if (type(msg) = "roUrlEvent")
                code = msg.GetResponseCode()
                if (code = 200)
                    playlist = CreateObject("roArray", 10, true)
                    json = ParseJSON(msg.GetString())
                    'print json
                    for each kind in json.editorials
-------------------------

But when placing it in scene graph something I do wrong, taking into account that it is HTTPS I do the following:

  m.readerTask = createObject("roSGNode", "ContentReader")
    m.readerTask.setField("uri", "https://.../metadata/x?limit=1")
    m.readerTask.observeField("content", "buildModel")
    m.readerTask.control = "RUN" 

I call the component that extends of task and I put the following:

 content = createObject("roSGNode", "ContentNode")
    contentxml = createObject("roXMLElement")
 
    readInternet = createObject("roUrlTransfer")
    readInternet.setUrl(m.top.uri)
    result = readInternet.getToString()
    json = ParseJSON(result)
                    for each kind in json.editorials

There the error described appears to me, is there any other way to do it or that advises me to correct
0 Kudos
EnTerr
Roku Guru

Re: Component Task to Consume Json

Trace the problem back step by step!
BRIGHTSCRIPT: ERROR: ParseJSON: Data is empty

is telling you that `result` is empty string - or at least is not JSON. Print `result` to check, then follow back what could be the reason - was the URL wrong, did you forget the SSL certificates etc
    readInternet = createObject("roUrlTransfer")
    readInternet.setUrl(m.top.uri)
    result = readInternet.getToString()
    json = ParseJSON(result)
                    for each kind in json.editorials
0 Kudos

Re: Component Task to Consume Json

Thank you very much, I followed your advice and giving different types of follow up and another few examples corrected the different errors, greetings
0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.