json parsing issues with base64 encoding

1,341 views
Skip to first unread message

Ian Roughley

unread,
Mar 6, 2009, 11:52:05 AM3/6/09
to boston-r...@googlegroups.com
Anyone have experience decoding Base64 in JSON? I'm using the json gem,
but it's failing with:

> puts JSON.parse('"name":InRocmVlOiI=')


I've googled around, but can't find whether base64 non-string values are
supported. At moment I'm guessing not, but wanted to see if anyone on
the group had a work around or knew of another gem/plug-in that handled
this case.

/Ian

gui...@rcn.com

unread,
Mar 6, 2009, 2:41:47 PM3/6/09
to boston-r...@googlegroups.com

What's the error ? I vaguely recall something like that having to do with the '=' at the end or something, though I don't know if it was with JSON or just base 64

Daniel Choi

unread,
Mar 6, 2009, 11:54:51 PM3/6/09
to Boston Ruby Group

'"name":InRocmVlOiI=' isn't valid json. I think you need to express
the name-value pair as a regular JavaScript object and quote the
Base64 part like any other string. You can decode the string after you
parse the JSON.

irb(main):015:0> require 'base64'
=> true
irb(main):017:0> puts Base64.decode64( JSON.parse
('{"name":"InRocmVlOiI="}')['name'] )
"three:"
=> nil

Ian Roughley

unread,
Mar 9, 2009, 9:45:46 AM3/9/09
to boston-r...@googlegroups.com
> irb(main):020:0> puts JSON.parse('"name":InRocmVlOiI=')
> JSON::ParserError: 574: unexpected token at '"name":InRocmVlOiI='
> from /var/lib/gems/1.8/gems/json-1.1.3/lib/json/common.rb:122:in
> `parse'
> from /var/lib/gems/1.8/gems/json-1.1.3/lib/json/common.rb:122:in
> `parse'
> from (irb):20
> from /var/lib/gems/1.8/specifications/activerecord-2.1.0.gemspec:5

Ian Roughley

unread,
Mar 9, 2009, 9:52:03 AM3/9/09
to boston-r...@googlegroups.com
Thanks.

Unfortunately this is a service I'm consuming, so making the base64 into
a string is not possible. I'll check with the service provider.

/Ian
Reply all
Reply to author
Forward
0 new messages