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.

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/

Json is valid.I have tested it.

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.

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.

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.

“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?

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

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?

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.

When posting code, please use [code][/code] tags.
  1. The code you posted runs on my Roku. However, the response returned is over 6MB! The documentation used to state that there was a limit of 64K for AsyncGetToString () responses, although it doesn’t say that any more. However, try using AsyncGetToFile() instead, then read the file into a string using ReadAsciiFile().

Thank you for your help.Heartly thankful.But it doesn’t help me.

Can you please paste the code how can I get data from web service.May be that will helpful to me.

I am using Roku Player 4200X and 7.0.My same code was working before 7.0 update .After update it is not working.That’s why I am in trouble.

You already posted code yourself that gets and parses a JSON string from a web service, using the khanacademy URL. Are you saying that that code doesn’t work for you? It works fine for me, exactly as you posted it. If something is failing for you, you will need to post an example of the code that is actually failing, rather than similar code that is not failing.

–Mark

“subhalaxmi” wrote:
This is my json response.


Guys help me

As Belltown said, the “Unknown identifier” error would occur if the JSON data itself is invalid, or not recognized, during tokenization.
Failing to quote a key name or a string value would be an example of that.

The error message should include the exact token that it failed on, again as Belltown demonstrated.

What does the error message print (including in the quotes) for the JSON that you said didn’t parse?

Thanks guys.Its a great feeling to be in this forum. Finally solved this.Actually there’s issue in json string.Some characters is getting assigned before jso string.I had removed it and then it worked.

Hi subha
i am facing the same issue while parsing the json it comes like unknown identifier , what was the string character within the response that caused this issue .

Thanks
Regards
Sechin

“subhalaxmi” wrote:
Thanks guys.Its a great feeling to be in this forum. Finally solved this.Actually there’s issue in json string.Some characters is getting assigned before jso string.I had removed it and then it worked.
Hi subha
i am facing the same issue while parsing the json it comes like unknown identifier , what was the string character within the response that caused this issue .

Thanks
Regards
Sechin