Anyone have problems parsing JSON?

55 views
Skip to first unread message

pgio

unread,
Mar 25, 2011, 2:30:07 AM3/25/11
to Helma
Specifically I'm trying to decode the signed_request that Facebook
returns from its Registration plugin. Splitting it into its two parts
and running debase64() on the second part works, though the resulting
string ends up with an extra quote mark. The result looks like JSON
but the parseJSON() method always returns false. It's definitely a
string, it looks well-formed, but it won't parse. Any ideas? Facebook
uses Base64Url encoding, might that gave something to do with it?

Pete

Simon Oberhammer

unread,
Mar 25, 2011, 4:13:50 AM3/25/11
to he...@googlegroups.com, pgio
Hi Pete,
can you post the JSON string you have trouble with?

You can try a browser's native JSON.parse() or jsonlint.com to verify the string is valid JSON.

greetings
 simon

pgio

unread,
Mar 25, 2011, 11:49:02 AM3/25/11
to Helma
Sure. Here's the response from Facebook after split(), debase64() and
unwrap():

{"algorithm":"HMAC-SHA256","expires":0,"issued_at":
1301030795,"oauth_token":"134488113278066
97196677e182b97d171eb039-1005861684
aKEPt10NIgkOrOMDESJgTqWsCcw","registration":{"name":"Pete
Giovagnoli","first_name":"Pete","last_name":"Giovagnoli","email":"pgio
\u0040earthlink.net"},"registration_metadata":
{"fields":"name,first_name,last_name,email"},"user":
{"country":"us","locale":"en_US"},"user_id":"xxxxxxxxxxxx"} "

Notice there's substantial whitespace before and after the string. And
I redacted my Facebook ID.

Minus the whitespace and the last quote mark this passes the JSON
parser at http://json.parser.online.fr/
I thought unwrap() would remove the newlines in this chunk of text but
it doesn't seem to do much. Curious.

Thanks Simon.

On Mar 25, 3:13 am, Simon Oberhammer <simon.oberham...@gmail.com>
wrote:

Simon Oberhammer

unread,
Mar 25, 2011, 12:00:13 PM3/25/11
to he...@googlegroups.com
are you sure you aren't cutting the json in half when you do the
split()? maybe facebooks does base64(JSON.stringify(obj)). thus if you
first split, then debase64() and finally parse() you will end up with
broken JSON.

i wouldn't do unwrap() - linebreaks are fine in json.

this is how i did it: http://pastebin.com/DpmMy39B

simon

pgio

unread,
Mar 25, 2011, 12:49:33 PM3/25/11
to Helma
Facebook returns a Base64Url string in two parts separated by a
period, so I split on that, so that shouldn't be the problem. (The
first part is an encoded hash I think.) As I said the string above
comes out after the split().debase64() combo.

I found a jar that does Base64 and Base64u and I dropped that into my
installation and tried to patch modules/core/String.js with a new
method to use it, but I keep getting type errors when I try to invoke
the functions that are supposed to be exposed.

It's an interesting problem.
Thanks,
Pete

On Mar 25, 11:00 am, Simon Oberhammer <simon.oberham...@gmail.com>
wrote:

pgio

unread,
Mar 25, 2011, 6:54:11 PM3/25/11
to Helma
HA! Got it. It's definitely the Base64Url encoding. When I figured out
how to wrap a Java library that handled base64u, I patched the String
prototype with a new method and got the Facebook stuff decoded
correctly. parseJSON() now spits out a proper JSON object.

The library is at http://mindprod.com/products1.html#BASE64

Thanks,
Pete

Reply all
Reply to author
Forward
0 new messages