2

I'm trying to write a simple Roku application.

When I load the JSON file via roURLTransfer ParseJSON function gives me BRIGHTSCRIPT: ERROR: ParseJSON: Unknown identifier.

If I load the JSON file via ReadAsciiFile("pkg:/feed/feed.json") it works.

The JSON file is the same and I'm pretty sure that my JSON is correct.

url = "http://192.168.1.36/misc/roku/ifilm/feed.json"
result = ""
timeout = 10000

ut = CreateObject("roURLTransfer")
ut.SetPort(CreateObject("roMessagePort"))
ut.SetURL(url)
if ut.AsyncGetToString()
    event = wait(timeout, ut.GetPort())
    if type(event) = "roUrlEvent"
        result = event.GetString()
    elseif event = invalid
        ut.AsyncCancel()
    else
        print "roUrlTransfer::AsyncGetToString(): unknown event"
    end if
end if

' `print result` shows the correct lintable JSON
' print result
' Next line gives me: BRIGHTSCRIPT: ERROR: ParseJSON: Unknown identifier
json = ParseJSON(result)

But putting the JSON file inside the app works:

feed = ReadAsciiFile("pkg:/feed/feed.json")
sleep(2000)

json = ParseJson(feed)

I need to load the data from the Internet and using the embedded version doesn't help me. Does anyone know what should I do to make it work?

Farid Rn
  • 3,167
  • 5
  • 39
  • 66

1 Answers1

4

The "Unknown identifier" error is usually because there's a character in the json string that ParseJson() does not support. The reason why ReadAsciiFile() works is likely because the function "cleans up" the json string by applying UTF-8 encoding.

A common character that's present at the beginning of some JSON responses that causes this issue is the unicode character Byte Order Mark (BOM)

If you google "byte order mark json" you'll see lots of cases where this affects other platforms as well.

You can just do a simple find and replace to get rid of that character before attempting to parse the string.

bomChar = Chr(65279)
if result.left(len(bomChar)) = bomChar ' Check if the string has the BOM char prefix
    result = result.replace(bomChar, "")
end if

If that doesn't work, then your response may have some other conflicting character, in that case I would advise using ifUrlTransfer::AsyncGetToFile() instead of AsyncGetToString() and then use ReadAsciiFile() which should guarantee a properly formatted json string every time (as long as your json is valid).

Alejandro Cotilla
  • 2,501
  • 1
  • 20
  • 35
  • Thank you for your answer; it was BOM. I'm having problem with Persian characters showing as squares in my application and thought to myself maybe using UTF-8 BOM might fix the issue, but no luck! – Farid Rn Apr 25 '20 at 07:19
  • Np! The squares you are seeing is likely because of the font you’re using. Use Google’s Noto font: https://www.google.com/get/noto/ – Alejandro Cotilla Apr 26 '20 at 05:39