-1

CLARIFICATION: The question here is, "how do I stop using <cfquery> to retrieve data and replace it with a call to an API?"

I have a sql query where I need help to convert it to cfscript. However as you can see I have tried converting it. But, I need some verification if I am on the right path to convert from sql query to cfscript. if not, can anyone help me by converting from sql query to cfscript? thanks for the help. here is my code.

CFSCRIPT:

<cfset jsonDatas = fileRead("c:\Users\Desktop\MyApi.json" )>      
<cfset jsonData = deserializeJSON(jsonDatas) />       
<cfif arrayLen(jsonData)>  
  <cfloop array="#jsonData#" index="prop">       
    <cfoutput>  
      <cfscript>  
        // writedump(jsonData);for (item in jsonData[1])   {  
          if (#prop.payGrade# == 0) {
                 #prop.divisionNbr#;        
         #prop.probationBeginDate#;        
         #prop.legacyStatus#;        
         #prop.payStep#;        
         #prop.creationDate#;           
          } 
  • 1
    I think you'll need to make the question more clear. Does the JSON file contain your query results? What are you really trying to accomplish? Some edits to the question should make it more understandable. – rrk May 22 '20 at 17:06
  • Sorry about that. Yes, my json file contains my query results. I just dont want to use query view and want to retrieve data from api url/file URL? So, I starting convert whatever my sql query was doing inside cfscript. – jonathon myers May 22 '20 at 17:08
  • 1
    Since the example above isn't 'runnable' - and we can't access your database or json files - I'm sure you can see how it would be difficult for someone else to know what the problem is :-) So ... what happens when you run the code? How is the result different from what you expected? See for tips : [How to ask a good question](https://stackoverflow.com/help/how-to-ask). – SOS May 22 '20 at 17:34
  • If you want I can post my full json file? Also, the result is shows the output from here: `
    Output:`, but I want to see if I am converting right from sql query to cfscript
    – jonathon myers May 22 '20 at 17:42
  • You keep saying "convert from sql query to cfscript", but ... we don't know exactly what you mean by that :-) I'm *guessing* the API data is in a different format, or maybe has different column names, and you're trying to convert the JSON into a query with the same columns? If so I'd ask why? i.e. What is it you're ultimately doing with the json, because it's possible a query isn't needed. Try and put together a small [standalone example](https://stackoverflow.com/help/minimal-reproducible-example) we can execute. (Obviously sanitize the data 1st!) – SOS May 22 '20 at 19:38
  • So, what I mean as convert from sql to cfscript. Is convert whatever the sql statement is doing to cfscript, does that make sense now? Like I am trying to do right now in my above code – jonathon myers May 22 '20 at 21:21
  • @jonathonmyers I've updated your question based on your comments. – Adrian J. Moreno May 24 '20 at 17:44
  • So what's wrong with the code you have? – SOS May 26 '20 at 02:23
  • I dont want to use query view anymore, but use api/url call instead .. I need help converting from the same way I am doing in query view to cfscript/rest api way – jonathon myers May 26 '20 at 03:21

3 Answers3

0

You are asking about two different things.

  • SQL defines the query run on the database.
  • CFSCRIPT is just a script style syntax for ColdFusion code (as opposed to tags).

Your example code is

  • Here is some SQL
  • Here I am reading JSON and converting it to a ColdFusion struct using script style code instead of tags.

Are you trying to go from currently calling a database, which returns data as a ColdFusion query object, to calling an API, which returns data as a JSON packet? You need to then convert the JSON data into the same or similar CF structures you currently use?

UPDATE: If you want to replace the existing query with an API call,

  • Does the API already exist?
  • Does it return the same data as the current query?
  • Is your team moving to APIs to decouple existing code?
  • Do you need to know HOW to convert a <cfquery> call to an API call?
Adrian J. Moreno
  • 14,350
  • 1
  • 37
  • 44
  • I think I am not explaining properly. So, what I am trying to do is dont want to use the cfquery at all anymore but use the api/url call. So, for example whatever I am doing in sql statement translate to cfhttp/cfscript, but dont use sql statement or cfquery anymore. – jonathon myers May 22 '20 at 22:49
  • And where would the API get its data? – James A Mohler May 23 '20 at 21:38
0

I am going to go for broke here. Do you have data in a DB and you just want to return a JSON representation of it? As in

<cfquery name="result">
    SELECT ...
    FROM whereever
</cfquery>

<cfoutput>#SerializeJSON(result, 'struct')#</cfoutput>

And then something else tries to consume this data? I have been looking and your questions, and I don't know if we are on the generating data side of the world, or in the consuming data side of the world.

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
  • No.. I think I am not explaining properly. So, what I am trying to do is dont want to use the cfquery at all anymore but use the api/url call. So, for example whatever I am doing in sql statement translate to cfhttp/cfscript, but dont use sql statement or cfquery anymore. – jonathon myers May 22 '20 at 22:49
  • So is this all client side processing? As in you have code loaded into a browser, and the browser needs to get data from an API. But we are not talking about creating an API, we are talking about creating browser code? – James A Mohler May 22 '20 at 23:36
  • yes, basically dont get the result from sql(view) anymore. just do the same logic by using api/url instead, by converting cfquery to cfscript/cfhttp – jonathon myers May 24 '20 at 00:15
-1

This is really a comment, but I need a lot of space to write it.

Are you looking to get data from a remote source and display it on a web page. Are you looking to do something like this?

enter image description here

I think we need to clarify which are client side technologies and which are server side technologies.

Update based on comments**

Consider a VueJS solution

<!-- Showing stuff on screen -->
<div id="app">
  {{ info }}
</div>

I can show stuff on the screen. This is similar, but not identical to #info#

<!-- Getting data -->
<cfscript>
new Vue({
  el: '#app',
  data () {
    return {
      info: null
    }
  },
  mounted () {
    axios
      .get('https://api.coindesk.com/v1/bpi/currentprice.json')
      .then(response => (this.info = response))
  }
})
<cfscript>

And this is getting data from a remote source and putting that data into a javascript variable.

Explaination

So why am I doing this with Javascript rather than ColdFusion? Javascript runs on the browser; ColdFusion runs on the server. If you want to consume an API on the browser, you have to use browser based technologies.

My example is in VueJS, but Angular and React are also options. It is a bit dated, but jQuery can do this kind of stuff too.

Code Source: https://v2.vuejs.org/v2/cookbook/using-axios-to-consume-apis.html

tony19
  • 125,647
  • 18
  • 229
  • 307
James A Mohler
  • 11,060
  • 15
  • 46
  • 72
  • I have expanded out my answer. Hope it helps – James A Mohler May 25 '20 at 20:44
  • are you using `VueJS`? if yes, but I am not using VueJS. I just want some guidance on my code above if its a good way to convert from sql query view to the api call by doing the same thing I am doing in the query view – jonathon myers May 27 '20 at 13:04
  • So you have web page, that calls an API. The web page has to take the JSON data it gets an iterate through it. It has to do things like `` . The technologies to do that are very different. Take a look at: https://stackoverflow.com/questions/51963481/how-to-use-v-for-to-render-a-table-using-vue-js This is probably what you are looking to do as far as the iteration. – James A Mohler May 27 '20 at 17:37
  • James, But I dont want to use vue though. Look at my `cfscript` see if I am doing the same way that I am doing in `cfquery`? is it good approach? – jonathon myers May 27 '20 at 17:40
  • I am not recommending Vue per se. I am saying this is a Javascript problem not a ColdFusion problem. The data is being processed on the browser. That is different from being processed on a web server and delivered to a browser. – James A Mohler May 27 '20 at 18:07
  • ok. But, is the `cfscript` doing the same thing as `cfquery` ? – jonathon myers May 27 '20 at 18:13
  • Ignoring the fact that array variables are not the same as query variables, they look the same. – James A Mohler May 27 '20 at 19:39
  • Let me answer it this way. There are (at least) two ways you will know it is right. 1. If you use a tool like Postman and you initiate data pulls and it can pull and parse the data as expected. 2. When you web page is pulling the data it is able to do so and is able to parse the data and get on the the screen. You will know it is right when it can be tested and shown to be right. – James A Mohler May 28 '20 at 19:07