Evaluating JSON in Safari

Just a quickie on parsing JSON strings in Safari.

I’m in the middle of a project developing a timesheet/rostering application, which uses a lot of Javascript. I have a series of web services that interact with jQuery. For reasons beyond the scope of this post, the web service returns a JSON string, which needs to be parsed into a JSON object, and then further processing is done on the object.

I had a bug where Safari was choking on the JSON string returned by my webservice, yet Firefox and IE were happy parsing it. As it turns out, my JSON string had a Javascript keyword in it; ‘break’.

Here is my JSON string (well, an example):

{employee: 'Joe', worked: true, periods: [{start: '09:00 AM', finish: '17:00 PM'}], break: {paid: true, hours: '1'}}

Safari was choking on the break in the above JSON string.

When I wrapped the attribute in single quotes, like so:

{employee: 'Joe', worked: true, periods: [{start: '09:00 AM', finish: '17:00 PM'}], 'break': {paid: true, hours: '1'}}

it was happy.

You can replicate this by using the below code snippet to parse out your JSON string quickly in Safari’s debug window:

var json = "{employee: 'Joe', worked: true, periods: [{start: '09:00 AM', finish: '17:00 PM'}], break: {paid: true, hours: '1'}}";
var obj = (new Function("return " + json))();

which should fail. If you add the single quotes around break though, it should work just fine.

This also fails for other Javascript keywords:

(new Function("return " + "{if: true}"))();

fails. If you wrap if in single quotes, it works.

I think the take away lesson is to always wrap your object attributes in a JSON string in single quotes, otherwise Safari will choke on it.

View Comments

ASP.NET GridView with an ObjectDataSource

Just a quick tip: I got the following exception today: If a data source does not return ICollection and cannot return the total row count, it cannot be used by the GridView to implement server-side paging. If you’re trying to return an IEnumerable from your select method, try changing it to a List or an [...]

View Comments

Going Dark

Nearly 12 months on from the start of my blog (the first post went up at the end of November 2008), I’m finding less and less time to do the quality kind of writing I have set out to do. I feel that, as I change, so too does my website need to change. As [...]

View Comments

Mario AI Competition 2009 – Getting Started

Programming is fun. That’s why I decided it would be a good career for me. I love solving puzzles, and I love the concept of making computers, these mysterious boxes of my childhood, do things. However, when you finally get to working as a professional programmer, the types of projects you get to work on [...]

View Comments

Configuring webHttpBinding When Using WCF with JQuery

Firstly, a disclaimer: I don’t have a lot of experience working with AJAX enabled WCF services, and from reading some of Rick Strahl’s posts on using JQuery with ASP.NET, I’m doing things in a really hackish and terrible manner. Hopefully my mistakes won’t impact the usefulness of this short tip. When working with JQuery and [...]

View Comments

Going Camping with CouchDB On OS X Tiger

With Snow Leopard being released at the end of September this year, Tiger is definitely starting to show it’s age. I’ve been putting off upgrading to Leopard for quite some time, not being able to justify the down time associated with upgrading an OS. This has had an unpleasant side effect of not being able [...]

View Comments

My Best Guesses About Wolfram Alpha

Following the “soft launch” of Wolfram Alpha a few days ago, I’ve been wondering exactly how Wolfram Alpha works. From the description of all the hype leading up to it, it seemed like a question answering application that is specialized in answering questions, as opposed to say Google, which is more into finding and presenting [...]

View Comments

Now Appearing In Austin, Texas

This blog has gone dark over the last two or so weeks, but for a very good reason. On the 21st of April, I flew from Perth, Western Australia to Austin, Texas, where I will be staying for the next 6 months. For those not acquainted with the trip, it’s half a world away at [...]

View Comments

Where Do We Draw The Line On Complexity?

I’ve got a few posts planned about AI and HCI, however these require a lot of time to research and write code for, time that I don’t have given I’m flying to the US on the 21st of April. So I apologise to people who saw my last AI article on Hacker News and Reddit [...]

View Comments

jQuery Context Menu Unbind Click Fix

Updated on May 28, 2009, due to partial fix. The fix should now be a complete fix. – Apologies for the somewhat confusing title. I’ve been doing a lot of jQuery work, replacing all the AJAX.NET crap in my project at work to speed the interface up. I’ve started using the awesome jQuery Context Menu [...]

View Comments