How to set order of SQL-response? <p>I've got a mule flow that queries a SQL-server database and then converts the response to JSON and then writes the JSON comma separated to file.</p> <p>My problem is that the order of the column in the written file doesn't match the order of the fields in the query. How tho define the order of columns in a query answer?</p> <p>The JSON to CSV conversion is like this:</p> <pre><code> public class Bean2CSV { /** * @param args * @throws JSONException */ public String conv2csv(Object input) throws JSONException { if (!input.equals(null)){ String inputstr = (String)input; JSONArray jsonArr = new JSONArray(inputstr); String csv = CDL.toString(jsonArr); csv = csv.replace(',', ';'); System.out.println(inputstr); //csv System.out.println(csv); //csv return csv; } else return "";} } </code></pre> <p>and here is the flow</p> <hr> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.mulesoft.org/schema/mule/jdbc" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:core="http://www.mulesoft.org/schema/mule/core" version="CE-3.4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule- http.xsd http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.mulesoft.org/schema/mule/jdbc http://www.mulesoft.org/schema/mule/jdbc/current/mule-jdbc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring- beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd"&gt; &lt;context:property-placeholder location="classpath:cognos_import.properties" ignore-resource-not-found="true"/&gt; &lt;spring:beans&gt; &lt;spring:bean id="dataSource" name="dataSource" class="org.enhydra.jdbc.standard.StandardDataSource" destroy-method="shutdown"&gt; &lt;spring:property name="driverName" value="net.sourceforge.jtds.jdbc.Driver"/&gt; &lt;spring:property name="user" value="${dbusername}"/&gt; &lt;spring:property name="password" value="${dbpassword}"/&gt; &lt;/spring:bean&gt; &lt;spring:bean id="changeDB" class="ChangeDatabase"&gt; &lt;spring:property name="serverip" value="${dbserverip}"/&gt; &lt;spring:property name="serverport" value="${dbserverport}"/&gt; &lt;spring:property name="dbprefix" value="${dbprefix}"/&gt; &lt;/spring:bean&gt; &lt;/spring:beans&gt; &lt;jdbc:connector name="db_conn" dataSource-ref="dataSource" validateConnections="false" pollingFrequency="5000" doc:name="Database"&gt; &lt;jdbc:query key="readbal" value="SELECT #[header:INBOUND:company] as company, AcNo as Konto , R2 as Avd, #[header:INBOUND:period] as Period, sum(AcAm) as Saldo FROM AcTr WHERE (AcYrPr &amp;lt;= #[header:INBOUND:period]) and (AcNo &amp;lt; 3000) GROUP BY AcNo,R2 ORDER BY AcNo,R2;"/&gt; &lt;jdbc:query key="readres" value="SELECT #[header:INBOUND:company] as company, AcNo as Konto , R2 as Avd, #[header:INBOUND:period] as Period, sum(AcAm) as Saldo FROM AcTr WHERE (AcYrPr &amp;lt;= #[header:INBOUND:period]) and (AcYrPr &amp;gt;= #[header:INBOUND:starttid]) and (AcNo &amp;gt;= 3000) GROUP BY AcNo,R2 ORDER BY AcNo,R2"/&gt; &lt;jdbc:query key="readiclr" value="SELECT #[header:INBOUND:company] as company, AcTr.AcNo as Konto , AcTr.R2 as Avd, #[header:INBOUND:period] as Period, sum(AcTr.AcAm) as Saldo,sum(AcTr.CurAm) as ValutaBel, AcTr.Cur as Valuta, AcTr.R1 as FtgID FROM AcTr LEFT JOIN Actor ON AcTr.R1=Actor.R1 WHERE (AcTr.AcYrPr &amp;lt;= #[header:INBOUND:period]) and (AcTr.AcYrPr &amp;gt;= #[header:INBOUND:starttid]) and Actor.SAcSet=2 and (AcTr.AcNo=3019 or AcTr.AcNo=3099 or AcTr.AcNo=3199 or AcTr.AcNo=3299 or AcTr.AcNo=3499 or AcTr.AcNo=3519 or AcTr.AcNo=3599 or AcTr.AcNo=3699 or AcTr.AcNo=3799 or AcTr.AcNo=3919 or AcTr.AcNo=3999 or AcTr.AcNo=4299 or AcTr.AcNo=4399 or AcTr.AcNo=4599 or AcTr.AcNo=4699 or AcTr.AcNo=4799 or AcTr.AcNo=5099 or AcTr.AcNo=5299 or AcTr.AcNo=5499 or AcTr.AcNo=5699 or AcTr.AcNo=5799 or AcTr.AcNo=5999 or AcTr.AcNo=6099 or AcTr.AcNo=6399 or AcTr.AcNo=6499 or AcTr.AcNo=6999 or AcTr.AcNo=7999 or AcTr.AcNo=8399 or AcTr.AcNo=8499) and AcTr.Sup&amp;lt;&amp;gt;0 GROUP BY AcTr.AcNo,AcTr.R2,AcTr.R1,AcTr.Cur"/&gt; &lt;jdbc:query key="readickr" value="SELECT #[header:INBOUND:company] as company, AcTr.AcNo as Konto , AcTr.R2 as Avd, #[header:INBOUND:period] as Period, sum(AcTr.AcAm) as Saldo,sum(AcTr.CurAm) as ValutaBel, AcTr.Cur as Valuta, AcTr.R1 as FtgID FROM AcTr LEFT JOIN Actor ON AcTr.R1=Actor.R1 WHERE (AcTr.AcYrPr &amp;lt;= #[header:INBOUND:period]) and (AcTr.AcYrPr &amp;gt;= #[header:INBOUND:starttid]) and Actor.CAcSet=2 and (AcTr.AcNo=3019 or AcTr.AcNo=3099 or AcTr.AcNo=3199 or AcTr.AcNo=3299 or AcTr.AcNo=3499 or AcTr.AcNo=3519 or AcTr.AcNo=3599 or AcTr.AcNo=3699 or AcTr.AcNo=3799 or AcTr.AcNo=3919 or AcTr.AcNo=3999 or AcTr.AcNo=4299 or AcTr.AcNo=4399 or AcTr.AcNo=4599 or AcTr.AcNo=4699 or AcTr.AcNo=4799 or AcTr.AcNo=5099 or AcTr.AcNo=5299 or AcTr.AcNo=5499 or AcTr.AcNo=5699 or AcTr.AcNo=5799 or AcTr.AcNo=5999 or AcTr.AcNo=6099 or AcTr.AcNo=6399 or AcTr.AcNo=6499 or AcTr.AcNo=6999 or AcTr.AcNo=7999 or AcTr.AcNo=8399 or AcTr.AcNo=8499) and AcTr.Cust&amp;lt;&amp;gt;0 GROUP BY AcTr.AcNo,AcTr.R2,AcTr.R1,AcTr.Cur"/&gt; &lt;jdbc:query key="readiclb" value="SELECT #[header:INBOUND:company] as company, AcTr.AcNo as Konto, AcTr.R2 as Avd, #[header:INBOUND:period] as Period, sum(AcTr.AcAm) as Saldo, sum(AcTr.CurAm) as ValutaBel, AcTr.Cur as Valuta, AcTr.R1 as FtgID FROM AcTr LEFT JOIN Actor ON AcTr.R1=Actor.R1 WHERE (AcTr.AcYrPr &amp;lt;= #[header:INBOUND:period]) and Actor.SAcSet=2 and AcTr.Sup&amp;lt;&amp;gt;0 and (AcTr.AcNo=1511 or AcTr.AcNo=2441) GROUP BY AcTr.AcNo,AcTr.R2,AcTr.R1,AcTr.Cur"/&gt; &lt;jdbc:query key="readickb" value="SELECT #[header:INBOUND:company] as company, AcTr.AcNo as Konto, AcTr.R2 as Avd, #[header:INBOUND:period] as Period, sum(AcTr.AcAm) as Saldo, sum(AcTr.CurAm) as ValutaBel, AcTr.Cur as Valuta, AcTr.R1 as FtgID FROM AcTr LEFT JOIN Actor ON AcTr.R1=Actor.R1 WHERE (AcTr.AcYrPr &amp;lt;= #[header:INBOUND:period]) and Actor.CAcSet=2 and AcTr.Cust&amp;lt;&amp;gt;0 and (AcTr.AcNo=1511 or AcTr.AcNo=2441) GROUP BY AcTr.AcNo,AcTr.R2,AcTr.R1,AcTr.Cur"/&gt; &lt;/jdbc:connector&gt; &lt;file:connector name="output" outputPattern=" #[function:datestamp:dd-MM-yy-hh-mm-ss]#[header:INBOUND:company].txt" autoDelete="false" outputAppend="true" streaming="false" validateConnections="false" doc:name="File"/&gt; &lt;message-properties-transformer name="delete-content-type-header" doc:name="Message Properties"&gt; &lt;delete-message-property key="Content-type"/&gt; &lt;/message-properties-transformer&gt; &lt;message-properties-transformer name="add-csv-content-type-header" doc:name="Message Properties"&gt; &lt;add-message-property key="Content-Type" value="text/csv"/&gt; &lt;/message-properties-transformer&gt; &lt;message-properties-transformer name="add-filename" doc:name="Message Properties"&gt; &lt;add-message-property key="Content-Disposition" value="attachment; filename=testfil.txt"/&gt; &lt;/message-properties-transformer&gt; &lt;flow name="CD-test1Flow1" doc:name="CD-test1Flow1"&gt; &lt;http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8082" doc:name="HTTP"/&gt; &lt;not-filter doc:name="Not"&gt; &lt;wildcard-filter pattern="*favicon*" caseSensitive="true"/&gt; &lt;/not-filter&gt; &lt;logger message="Log1 #[message.getPayload()]! " level="INFO" doc:name="Logger1"/&gt; &lt;http:body-to-parameter-map-transformer doc:name="Body to Parameter Map"/&gt; &lt;component class="ValidationService3x" doc:name="Java"/&gt; &lt;echo-component doc:name="Echo"/&gt; &lt;set-variable variableName="Filename" value=" #[function:datestamp:dd-MM-yy-hh-mm-ss]-#[header:INBOUND:company]-#[header:INBOUND:period]" doc:name="FilenameVar"/&gt; &lt;component doc:name="Java"&gt; &lt;spring-object bean="changeDB"/&gt; &lt;/component&gt; &lt;all doc:name="All"&gt; &lt;processor-chain&gt; &lt;jdbc:outbound-endpoint exchange-pattern="request-response" queryKey="readbal" connector-ref="db_conn" doc:name="DB"/&gt; &lt;expression-filter expression="payload.size() &amp;gt; 0" doc:name="not empty"/&gt; &lt;json:object-to-json-transformer doc:name="Object to JSON"/&gt; &lt;component class="Bean2CSV" doc:name="Java"/&gt; &lt;echo-component doc:name="Echo"/&gt; &lt;file:outbound-endpoint path="${outputfolder}" outputPattern="#[flowVars['Filename']]-bal.csv" connector-ref="output" doc:name="File"/&gt; &lt;/processor-chain&gt; &lt;processor-chain&gt; &lt;jdbc:outbound-endpoint exchange-pattern="request-response" queryKey="readres" connector-ref="db_conn" doc:name="DB100-ickr"/&gt; &lt;expression-filter expression="payload.size() &amp;gt; 0" doc:name="not empty"/&gt; &lt;json:object-to-json-transformer doc:name="Object to JSON"/&gt; &lt;component class="Bean2CSV" doc:name="Java"/&gt; &lt;echo-component doc:name="Echo"/&gt; &lt;file:outbound-endpoint path="${outputfolder}" outputPattern="#[flowVars['Filename']]-res.csv" connector-ref="output" doc:name="File"/&gt; &lt;/processor-chain&gt; &lt;processor-chain&gt; &lt;jdbc:outbound-endpoint exchange-pattern="request-response" queryKey="readiclb" connector-ref="db_conn" doc:name="DB100-ickr"/&gt; &lt;expression-filter expression="payload.size() &amp;gt; 0" doc:name="not empty"/&gt; &lt;json:object-to-json-transformer doc:name="Object to JSON"/&gt; &lt;component class="Bean2CSV" doc:name="Java"/&gt; &lt;echo-component doc:name="Echo"/&gt; &lt;file:outbound-endpoint path="${outputfolder}" outputPattern="#[flowVars['Filename']]-icb.csv" connector-ref="output" doc:name="File"/&gt; &lt;/processor-chain&gt; &lt;processor-chain&gt; &lt;jdbc:outbound-endpoint exchange-pattern="request-response" queryKey="readickb" connector-ref="db_conn" doc:name="DB100-ickr"/&gt; &lt;expression-filter expression="payload.size() &amp;gt; 0" doc:name="not empty"/&gt; &lt;json:object-to-json-transformer doc:name="Object to JSON"/&gt; &lt;component class="Bean2CSVnotfirstline" doc:name="Java"/&gt; &lt;echo-component doc:name="Echo"/&gt; &lt;file:outbound-endpoint path="${outputfolder}" outputPattern="#[flowVars['Filename']]-icb.csv" connector-ref="output" doc:name="File"/&gt; &lt;/processor-chain&gt; &lt;processor-chain&gt; &lt;jdbc:outbound-endpoint exchange-pattern="request-response" queryKey="readiclr" connector-ref="db_conn" doc:name="DB100-ickr"/&gt; &lt;expression-filter expression="payload.size() &amp;gt; 0" doc:name="not empty"/&gt; &lt;json:object-to-json-transformer doc:name="Object to JSON"/&gt; &lt;component class="Bean2CSV" doc:name="Java"/&gt; &lt;echo-component doc:name="Echo"/&gt; &lt;file:outbound-endpoint path="${outputfolder}" outputPattern="#[flowVars['Filename']]-icr.csv" connector-ref="output" doc:name="File"/&gt; &lt;/processor-chain&gt; &lt;processor-chain&gt; &lt;jdbc:outbound-endpoint exchange-pattern="request-response" queryKey="readickr" connector-ref="db_conn" doc:name="DB100-ickr"/&gt; &lt;expression-filter expression="payload.size() &amp;gt; 0" doc:name="not empty"/&gt; &lt;json:object-to-json-transformer doc:name="Object to JSON"/&gt; &lt;component class="Bean2CSVnotfirstline" doc:name="Java"/&gt; &lt;echo-component doc:name="Echo"/&gt; &lt;file:outbound-endpoint path="${outputfolder}" outputPattern="#[flowVars['Filename']]-icr.csv" connector-ref="output" doc:name="File"/&gt; &lt;/processor-chain&gt; &lt;/all&gt; &lt;set-payload value="Nu är 100 tankat till #[flowVars['Filename']] " doc:name="Set Payload"/&gt; &lt;http:response-builder doc:name="HTTP Response Builder"/&gt; &lt;/flow&gt; </code></pre> <p></p>