AdobeStock_455007340

Working With JSON In Scorpio

There have been some comments on other entries here regarding JSON. JSON is a data interchange format, and is primarily used as a way to package data for use with Ajax development.
ColdFusion 8 (aka Scorpio) adds support for JSON in several ways:

  • For starters, two new functions named SerializeJSON() and DeserializeJSON() make it easy to convert data to and from JSON. Simple variables (numbers and strings), dates, arrays, structures, and even queries can be converted back and forth. ColdFusion queries get converted into JSON objects with two entries, COLUMNS contains a list of columns, and DATA is an array of arrays containing the actual data.
  • A supporting IsJSON() function does exactly what its name suggests.
  • In addition, CFC methods can now be instructed to return data serialized as JSON by specifying returnformat=”json”. This is perfect for Ajax controls that invoke CFC methods to retrieve data.

12 responses to “Working With JSON In Scorpio”

  1. tanguyr Avatar
    tanguyr

    Excellent. Just excellent.
    May sound strange to you, but this is the first new scorpio feature i hear about that *really* grabs my interest. I could drop this into my code TODAY.
    /t

  2. JesterXL Avatar
    JesterXL

    That’s just awesome! Really cool new feature.

  3. ReZz Avatar
    ReZz

    Hi Ben, I may be completely off here, but I’m just wondering what the difference would be between using CFWDDX with the CFML2JS action and this new SerializeJSON() function (aside from being shorter)?

  4. Steve Nelson Avatar
    Steve Nelson

    Rez-
    From CF to the browser would work very similar on the cf side. ( btw, I’m just assuming this is how it’ll work having done both JSON and WDDX work in the past)
    For example:
    <cfset mystruct=structnew()>
    <cfset mystruct.name="Steve">
    <cfwddx action="WDDX2JS" input="#mystruct#" output="mystructJS" toplevelvariable="myStruct">
    <script>
    <cfoutput>#mystructJS#</cfoutput>
    </script>
    vs:
    <script>
    <cfoutput>var mystruct=#SerializeJSON(myStruct)#</cfoutput>
    </script>
    Going from JS back to CF is a little trickier.
    With WDDX, you have to include the WDDX.js file, create a new Serializer, serialize it, then send it via xmlhttp back to CF and call the cfwddx tag.
    With JSON, it’s a similar process. you serialize the object with one of the various json libraries out there, and send it back to cF and then call the deserialize() function.
    So these two processes are almost identical in implementation, the difference is that JSON is dramatically less markup required and doesn’t need a parser on the javascript side.
    Did that help?

  5. Steve Nelson Avatar
    Steve Nelson

    dang i wish more blogs had previews. Let me post that code again and see what happens:
    <cfset mystruct=structnew()>
    <cfset mystruct.name="Steve">
    <cfwddx action="WDDX2JS" input="#mystruct#" output="mystructJS" toplevelvariable="myStruct">
    <script>
    <cfoutput>#mystructJS#</cfoutput>
    </script>
    vs:
    <script>
    <cfoutput>var mystruct=#SerializeJSON(myStruct)#</cfoutput>
    </script>

  6. Thomas Messier Avatar
    Thomas Messier

    Guess that’ll spell the end of CFJSON, and that’s not a bad thing! One question Ben, when you serialize a query will it make it an array of structures or will it keep it as a structure of arrays? I hope the former because it’s much more practical most of the time.

  7. Ben Forta Avatar
    Ben Forta

    Thomas, it’s an object containing two parallel arrays, one of which DATA) is an array of arrays.
    — Ben

  8. Thomas Messier Avatar
    Thomas Messier

    It would have helped if I had read the bloody post properly… Thanks for still indulging me with a reply! I’ve really been enjoying the progressive release of Scorpio features, can’t wait to see the full-blown product.

  9. Joshua Curtiss Avatar
    Joshua Curtiss

    Jeez. We’re all starting to sound like a broken record–I realize this–but these new Scorpio features are insanely great. I concur with tanguyr.. I’ve been around since CF4, and sure, CFMX was a revolutionary change, but Scorpio is the release that has the most developer goodies to get excited about.. All of that immediately applicable stuff that fills needs we’re aware of right now. You guys are RIGHT ON TARGET with stuff that I want.

  10. Michael Long Avatar
    Michael Long

    ReturnFormat="JSON"
    Interesting that you’re providing automatic CFC conversions. Are you doing the same for XML and/or WDDX?

  11. Ben Forta Avatar
    Ben Forta

    Michael, yes, ReturnFormat="WDDX" is also supported (which I suppose gives you XML as both JSON and WDDX are that)
    — Ben

  12. Juris Avatar
    Juris

    Hi Ben!
    Maybe You can answer WHEN serializeJSON will be fixed?
    Simple example:
    <cfset test = {str=’no’}>
    <cfset json = serializeJSON(test)>
    <cfset fromJSON = deserializeJSON(json)>
    <cfoutput>
    #test.str#<br>
    #json#<br>
    #fromJSON.str#
    </cfoutput>
    Resulting JSON contains false ???? What, Why??
    Maybe You can provide some advice how to fix it?
    For example country Norway has a country code ‘no’, after serialization becomes completely useless…

Leave a Reply