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: 
subhalaxmi
Visitor

ParseJson Error

Hi Guys

I am parsing a json data.I am using Roku OS 7.0

my code :
if (code = 200)
json = ParseJSON(msg.GetString())

But it throws ParseJson:Unknown Identifier Error. How can I solve this guys. I need urgent response.
0 Kudos
16 REPLIES 16
TheEndless
Channel Surfer

Re: ParseJson Error

Without sharing the json string you're trying to parse, it's impossible to help. Based on the message, it sounds like the json is invalid.

You could try a tool like this to validate your json, and identify the issue: http://jsonlint.com/
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
subhalaxmi
Visitor

Re: ParseJson Error

Json is valid.I have tested it.
0 Kudos
TheEndless
Channel Surfer

Re: ParseJson Error

Can you share the URL you're pulling the JSON from, so we can investigate? Again, we can't really help without knowing what "msg.GetString()" returns.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
belltown
Roku Guru

Re: ParseJson Error

Maybe you don't have quote characters around an object key:


' GOOD ...
jsonString = "{" + Chr (34) + "a" + Chr (34) + ": 42}"
json = parseJson (jsonString)
Print json

' BAD ...
jsonString = "{a: 42}"
json = parseJson (jsonString)
Print json



a: 42

BRIGHTSCRIPT: ERROR: ParseJSON: Unknown identifier 'a: 42}': pkg:/source/Main.brs(18)
invalid


Or it could be some other problem with your Json. But like TheEndless says, without seeing the Json string there's not much help we can offer.
0 Kudos
subhalaxmi
Visitor

Re: ParseJson Error

Sorry I cannot share the json url.But I can assure you that json is valid in rest client.And I am using the follwoing code to parse.

if (request.AsyncGetToString())
while (true)
msg = wait(0, port)
if (type(msg) = "roUrlEvent")
code = msg.GetResponseCode()
if (code = 200)
print msg.GetString()
json = ParseJSON(msg.GetString())
return json.itemCount.ToInt()
endif
else if (event = invalid)
request.AsyncCancel()
endif
end while
endif


when I am printing msg.GetString() it is printing all data which is in valid json format.But in next line json = ParseJSON(msg.GetString()) it is breaking after parsing some codes.

I am bugging since 4 hours.I ccannot be able to find out issue.Can you please help me.
0 Kudos
TheEndless
Channel Surfer

Re: ParseJson Error

"subhalaxmi" wrote:
I ccannot be able to find out issue.Can you please help me.

I honestly can't without knowing what you're trying to parse. Can you share it via PM?
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
subhalaxmi
Visitor

Re: ParseJson Error

This is my json response.

{
"status": 200,
"msg": "OK",
"list": [{
"0": "0",
"id": "1021",
"ispresent": 0
}, {
"0": "1",
"id": "1022",
"ispresent": 0
}, {
"0": "2",
"id": "1023",
"ispresent": 0
}, {
"0": "3",
"id": "1024",
"ispresent": 0
}, {
"0": "4",
"id": "1025",
"ispresent": 0
}, {
"0": "5",
"id": "1026",
"ispresent": 0
}, {
"0": "6",
"id": "1027",
"ispresent": 0
}, {
"0": "7",
"id": "1028",
"ispresent": 0
}, {
"0": "8",
"id": "1029",
"ispresent": 0
}, {
"0": "9",
"id": "1030",
"ispresent": 0
}],
"orderby": "",
"count": "10",
"limit": 10
}

Guys help me
0 Kudos
belltown
Roku Guru

Re: ParseJson Error

It parses correctly on my Roku 2XS running 7.0 build 9037.

All I can think of is that there may be extraneous unprintable characters (e.g. cr/lf) in your Json response.

Also, what is the character encoding of your response?
0 Kudos
subhalaxmi
Visitor

Re: ParseJson Error

Can you please post your parsing code you use to get response.


Function get_playlist() as object
request = CreateObject("roUrlTransfer")
port = CreateObject("roMessagePort")
request.SetMessagePort(port)
request.SetUrl("http://www.khanacademy.org/api/v1/playlists")
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())
for each kind in json
topic = {
ID: kind.id
Title: kind.standalone_title
}
playlist.push(topic)
end for
return playlist
endif
else if (event = invalid)
request.AsyncCancel()
endif
end while
endif
return invalid
End Function


I am using this kind of code which needs no encoding.
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.