Home > Software engineering >  ColdFusion : String : Get Price From Inside 2 Points
ColdFusion : String : Get Price From Inside 2 Points

Time:02-11

I'm playing with the NOMICS API and get data in a string. But I'm having trouble getting just the Price:

This is part of the string from the METHOD=GET - which works fine..

"currency":"SHIB","platform_currency":"ETH","price":"0.000026199726","price_date":"2022-02-06T00:00:00Z","price_timestamp":"

I know that ,"price":" is the lead and then "," is the end... But I can't seem to get just the 0.000026199726 from the middle- which is what I need.

<CFHTTP METHOD="Get"
     URL="https://api.nomics.com/v1/currencies/ticker?key=#apikey#&ids=SHIB">

<cfset feedData = cfhttp.filecontent>

<cfset startpos = findNoCase(',"price":"', feedData)>
<cfset endpos = findNoCase('",', feedData)>
<cfset getdata = mid(feeddata,startpos,endpos-startpos)

<b>#getdata#</b> Errors as neg number.

The value of parameter 3 of the function Mid, which is now -191, must be a non-negative integer

This has to be an easy task. I must be using the wrong string function?

EDIT: Figured out - it was finding the "," but they are so many of them it found first one, which put things negative - so fix was to find the structure after. ","price_date" is after.

      <cfset string = cfhttp.filecontent>
      <cfset startpos = findNoCase('price":"', string)>
      <cfset endpos = findNoCase('","price_date"', string)>
      <cfset detdata = mid(string,startpos,endpos-startpos)>

      <cfoutput>
          start: #startpos#<br>
          end: #endpos#<br>
          data: #detdata#<br>
          trimmed data: #trim(detdata)#<br>
          trimmed data: 
          <br><b>#removechars(detdata,1,8)#</b><br><br>
      </cfoutput>

I'll look at the JSON examples as well. Perhaps that will help with multiple pulls.

Excellent Folks : Thank you so much

      <CFHTTP METHOD="Get"
URL="https://api.nomics.com/v1/currencies/ticker?key=#apikey#&ids=SHIB,BTC">

      <cfset output = cfhttp.filecontent>
      <cfoutput>
      <cfset arrayOfStructs = deserializeJson(output)>
      <cfloop array="#arrayOfStructs#" index="getpr">
            <cfset Price = getpr.price />
            <cfset TKID = getpr.id />
            #tkid#: #price#<br>
      </cfloop>
      </cfoutput>

Spits out:

BTC: 43963.45841296

SHIB: 0.000033272664

CodePudding user response:

Credit to Andrea/SOS

  <CFHTTP METHOD="Get"
      URL="https://api.nomics.com/v1/currencies/ticker?key=#apikey#&ids=SHIB,BTC">

  <cfset output = cfhttp.filecontent>
  <cfoutput>
  <cfset arrayOfStructs = deserializeJson(output)>
  <cfloop array="#arrayOfStructs#" index="getpr">
        <cfset Price = getpr.price />
        <cfset TKID = getpr.id />
        #tkid#: #price#<br>
  </cfloop>
  </cfoutput>
  • Related