Converting a .CSV file to a query and specifying custom column names using the CFHTTP tag in ColdFusion

Readability

Converting a .CSV file to a query and specifying custom column names using the CFHTTP tag in ColdFusion

In a pre­vi­ous exam­ple, “Con­vert­ing a .CSV file to a query using the CFHTTP tag in Cold­Fu­sion”, we saw how you could con­vert an exter­nal .CSV (comma sep­a­rated val­ues) file into a query using the <CFHTTP> tag in Cold­Fu­sion and set­ting the name, url, and firstRowAsHeaders attributes.

The fol­low­ing exam­ple shows how you can over­ride the CSV file header row with cus­tom col­umn names for the query by spec­i­fy­ing the columns attribute on the <CFHTTP> tag in ColdFusion.




The exter­nal .CSV file, art.csv, is as follows:

"ARTID","ARTISTID","ARTNAME","DESCRIPTION","ISSOLD","LARGEIMAGE","MEDIAID","PRICE"
"1","1","charles1","Pastels/Charcoal","1","aiden01.jpg","1","10000"
"2","1","Michael","Pastels/Charcoal","0","aiden02.jpg","1","13900"
"3","1","Freddy","Pastels/Charcoal","1","aiden03.jpg","1","12500"
"4","1","Paulo","Pastels/Charcoal","1","aiden04.jpg","1","11100"
"5","1","Mary","Pastels/Charcoal","1","aiden05.jpg","1","13550"

And the out­put of the <CFDUMP> tag is as fol­lows:

In a previous example, “Converting a .CSV file to a query using the CFHTTP tag in ColdFusion”, we saw how you could convert an external .CSV (comma separated values) file into a query using the <CFHTTP> tag in ColdFusion and setting the name, url, and firstRowAsHeaders attributes.

The following example shows how you can override the CSV file header row with custom column names for the query by specifying the columns attribute on the <CFHTTP> tag in ColdFusion.

<cfhttp name="artFromCSV"
        url="http://live.coldfusionexamples.com/art.csv"
        columns="ArtKey,ArtistKey,NameOfTheArt,DescOfTheArt,ArtIsSold,ArtImageLarge,MediaKey,HowMuchForTheArtBub"
        firstRowAsHeaders="true"
        method="get" />
 
<cfdump var="#artFromCSV#" />

The external .CSV file, art.csv, is as follows:

"ARTID","ARTISTID","ARTNAME","DESCRIPTION","ISSOLD","LARGEIMAGE","MEDIAID","PRICE"
"1","1","charles1","Pastels/Charcoal","1","aiden01.jpg","1","10000"
"2","1","Michael","Pastels/Charcoal","0","aiden02.jpg","1","13900"
"3","1","Freddy","Pastels/Charcoal","1","aiden03.jpg","1","12500"
"4","1","Paulo","Pastels/Charcoal","1","aiden04.jpg","1","11100"
"5","1","Mary","Pastels/Charcoal","1","aiden05.jpg","1","13550"

And the output of the <CFDUMP> tag is as follows:

Leave a Reply