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.