1 Reply Latest reply on Apr 25, 2014 12:01 PM by Carl Von Stetten

    How to fix a problem with the order of strings in a JSON response for a CFHTTP request?

    OsmarFraga

      Good morning, guys! (It's 10:50 a.m. in Brazil)

       

      First of all, I'm still new at CF and my questions may seem too much silly and my English's not the best, so, please be patient with me. hehe

       

      Well, I'm accessing a link via CFHTTP that gives me a JSON response. I'm not familiar with JSON yet, so I'm working it as a string and using string functions (Find,RemoveChars,Replace,etc), but it happens to me that I'm receiving a certain kind of order of the strings list from the JSON when I access it directly from the browser and other kind of order totally different when I access it from the CFHTTP.

       

      I only figured it out because I was working at home with my code and there everything went just fine as expected. Then I brought to my job to develop a little bit more as soon as I get a free time and here my code doesn't work anymore. When I took a look at the JSON by a CFOUTPUT, I realized the strings were in a different order.

       

      Does anyone know why is it happening?

       

      Link to the JSON: http://sigmine.dnpm.gov.br/ArcGIS/rest/services/extra/dados_dnpm/MapServer/0/query?text=83 0620/2012&geometry=&geometryType=esriGeometryEnvelope&inSR=&spatialRel=esriSpatialRelInter sects&where=&returnGeometry=true&outSR=&outFields=FID,Shape,PROCESSO,ID,NUMERO,ANO,AREA_HA ,FASE,ULT_EVENTO,NOME,SUBS,USO,UF&f=pjson

       

      Basic code simplified to find out that the order of strings were coming different when access by CFHTTP:

       

      <cfhttp url="#URLAbove#" method="get" result="DadosDoDNPM" charset="utf-8" timeout="10000" />

      <cfset DadosJSON = deserializeJSON(DadosDoDNPM.FileContent) />

      <cfif arrayLen(DadosJSON.features)>

          <cfset CodigoFonteJSON = ToString(serialize(DadosJSON.features)) />   

          <cfoutput> #CodigoFonteJSON# </cfoutput>

      </cfif>

       

      Is there a way to fix it and to make my code work everywhere?

      If it matters, I use Railo at home and at my job.

       

      Since now, I thank you.

        • 1. Re: How to fix a problem with the order of strings in a JSON response for a CFHTTP request?
          Carl Von Stetten Adobe Community Professional & MVP

          When you deserialize JSON data, ColdFusion converts it into native structures and arrays (roughly equivalent to JavaScript objects and arrays).  In ColdFusion, structures are unordered (that is, they don't maintain keys in any particular ordered sequence).

           

          This really shouldn't be a problem though, at least not if you just want to work with the data contained in the deserialized JSON structure.  So DadosJSON should be a native ColdFusion structure, and you can interact with it like any other structure in ColdFusion.  In fact, you should not have to re-serialize it at all unless you need to send it to an external application using JSON.

           

          Why are you reserializing the data?  And why use serialize() instead of serializeJSON()?

           

          -Carl V.