One example of dealing with JSON deserialization issues

This post is more than 2 years old.

There have been a few blog posts recently about issues with JSON and ColdFusion. Specifically issues concerning data being "transformed" into other data types. While there are a variety of ways to solve this issue, I thought I'd share one particular example, and one particular solution, I shared with a reader earlier today.

The reader was dealing with JSON returned from Facebook. This JSON packet contained a key called actor_id. When the data was translated from JSON some corruption occurred. You can see an example of this here. Notice how the third actor's ID was changed to scientific notation:

I took a look at the JSON string and noticed that the actor IDs all took this form:


I decided to simply wrap the value portion in quotes using regex:

<cfset json = rereplace(json, '"actor_id":(.*?)([,}])', '"actor_id":"\1"\2', "all")>

Notice that the end of my regex matches either a comma or a } to handle it being in a middle position of the object at the end. (I also had to do this for a UID value that was being corrupted.) The result clearly shows that this helped:

I really, really like JSON, but I'm thinking that those of us who make use of it will need to be vigilant for issues like this. Considering that only some of the actor IDs values were transformed, it's easily something that could be missed earlier in development.

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA

Archived Comments

Comment 1 by Nathan Mische posted on 1/29/2010 at 7:36 AM

Well, in this case DeserializationJSON was actually keeping the correct data type. This is actually an issue with how CF converts large numbers to string on output. The approach I probably take here is to use NumberFormat. NumberFormat(myArray[1].actor_id,"0") should do the trick.

Comment 2 by Nathan Mische posted on 1/29/2010 at 7:38 AM

Um, that should be DeserializeJSON, not DeserializationJSON...

Comment 3 by Ben Nadel posted on 1/29/2010 at 7:57 PM

This is frustrating.