Skip to main content
September 16, 2010
Question

I have no idea how to send someone a JSON file

  • September 16, 2010
  • 1 reply
  • 1008 views

I have a user that is requesting to query my DB and send the results to him in JSON format. I have no idea how to do this. I googled JSON and see it is  (JavaScript Object Notation. So the user wants the query results in this type of file. But I cant find how Coldfusion can do this. I am new to using coldfusion to transfer or manipulate files, so I am not clear on how to funnel the query results into a a file in JSON format and then how do I make it available for my user to grab. lol, I am not even sure what if I am asking the right stuff.

I am using coldfusion 8 and sql server 2000

    This topic has been closed for replies.

    1 reply

    ilssac
    Inspiring
    September 16, 2010

    You create a CFML page.

    This page quries your database and returns the results in JSON format intead of the more common HTML format.  You then give this person the URL to that CFML page.  The ColdFusion 8, Serialize JSON format should make that pretty trival.  http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=functions_s_03.html

    September 16, 2010

    This helps because I wasnt sure how ther user would get the information. Since I just provide them with the link, that answers a question for me.

    But How do I return the query results in JSON format. I can see the example on the page you linked to me:

    <cfscript>
        // Construct a weather query with information on cities.
        // To simplify the code, we use the same weather for all cities and days.
        // Normally this information would come from a data source.
        weatherQuery = QueryNew("City, Temp, Forecasts");
        QueryAddRow(weatherQuery, 2);
        theWeather=StructNew();
        theWeather.High=73;
        theWeather.Low=53;
        theWeather.Weather="Partly Cloudy";
        weatherArray=ArrayNew(1);
        for (i=1; i<=5; i++) weatherArray=theWeather;
        querySetCell(weatherQuery, "City", "Newton", 1);
        querySetCell(weatherQuery, "Temp", "65", 1);
        querySetCell(weatherQuery, "ForeCasts", weatherArray, 1);
        querySetCell(weatherQuery, "City", "San Jose", 2);
        querySetCell(weatherQuery, "Temp", 75, 2);
        querySetCell(weatherQuery, "ForeCasts", weatherArray, 2);
    
        // Convert the query to JSON.
        // The SerializeJSON function serializes a ColdFusion query into a JSON
        // structure.
        theJSON = SerializeJSON(weatherQuery);
        
        // Wrap the JSON object in a JavaScript function call.
        // This makes it easy to use it directly in JavaScript.
        writeOutput("onLoad( "&theJSON&" )");
    </cfscript>

    But CFSCRIPT is also something I have never used. What kind of language does it use? It looks like the query is
    embedded within the script, does it have to be done this way? Can't I use <CFQUERY> and then somehow output
    the data in JSON format, or send the output to a file or something?
    ilssac
    Inspiring
    September 16, 2010

    CFScript is just another way to write ColdFusion code.  Everything there could be written with CFML tags if you like.

        <cfset weatherQuery = QueryNew("City, Temp, Forecasts")>
        <cfset QueryAddRow(weatherQuery, 2)>
        <cfset theWeather=StructNew()>
        ...etc...

    Plus, most of that code is just a way to hand build a query record set object without worying about any real database.

    Your code would most likely replace that with a single

    <cfquery....>

       ...

    </cfquery>


    block