-1

I am trying to output certain key values to show(WHICH I AM GETTING ALREADY). But for some reason it is returning as a struct. Basically, I am converting from query statement into api call. But for some reason I cant get it return from cfoutput to show as a struct from the api call or a json file. Can anyone tell me what I have done wrong. thanks for the help. here is my code:

Output: I get all my json fields showing in a struct. (with cfscript uncommented out)

Update output with current code: (I get my output of certain key value, but can I do iterate through a loop, I know the way I have it is not the best way)

<cfset jsonDatas = fileRead("c:\Users\Desktop\myApi.json" )>     
<cfset jsonData = deserializeJSON(jsonDatas) />      
<cfdump var="#jsonData#" abort="true">  


   <cfloop array="#jsonData#" index="prop">  
    <cfoutput>
    <br>Output: 
    #prop.employeeId#
    .....

    </cfoutput>
     </cfloop>  

My Json:

    [ 
{ 
 "employeeId" : "77777",  
"lastName" : "DOE",  
"firstName" : "JOHN",  
"middleName" : null,  
"sex" : "Male",  
"jobStatus" : "Active", 
"jobStatusDate" : "2020-01 03 00:00:00.0",  
"departmentNbr" : "5555",
}
]
James A Mohler
  • 11,060
  • 15
  • 46
  • 72

1 Answers1

0

Let's see if the third try does the trick.

I added more data so that we can see that each array is processed separately.

<cfscript>
    json =

     '[ 
{ 
 "employeeId" : "77777",  
"lastName" : "DOE",  
"firstName" : "JOHN",  
"middleName" : null,  
"sex" : "Male",  
"jobStatus" : "Active", 
"jobStatusDate" : "2020-01 03 00:00:00.0",  
"departmentNbr" : "5555"
},
{ 
 "employeeId" : "123",  
"lastName" : "Doe",  
"firstName" : "Jane",  
"middleName" : "H",  
"sex" : "Female",  
"jobStatus" : "Active", 
"jobStatusDate" : "2021-01 03 00:00:00.0",  
"departmentNbr" : "14"
}
]';

jsonData = DeserializeJSON(json);

writedump(jsonData);

for (row in jsonData)   {
    writeoutput("<hr />");
    for (item in row)   {
        writeoutput(item & ": " & row[item] & "<br />");
    }
}

</cfscript>

See: https://cffiddle.org/app/file?filepath=ca5836f8-af85-4878-a2d7-9406bdd4a884/dc41f7b8-3792-4141-bd0b-7a9839900a43/97dfe9de-53f2-4e82-8e8c-53fd5b5bfd36.cfm

Or From a file

<cfscript>

jsonDatas = fileRead("c:\Users\Desktop\myApi.json" );  
jsonData = deserializeJSON(jsonDatas);

writedump(jsonData);

for (row in jsonData)   {
    writeoutput("<hr />");
    for (item in row)   {
        writeoutput(item & ": " & row[item] & "<br />");
    }
}

</cfscript>
James A Mohler
  • 11,060
  • 15
  • 46
  • 72
  • So, remove the json = (properties) and call the json file itself. Thats what I need. not `json = ....` – jonathon myers May 20 '20 at 22:35
  • Exactly. The only reason I did it that way is so that you could see the results on cffiddle – James A Mohler May 21 '20 at 04:21
  • correct. thanks for that. But, can you change to call to a file? – jonathon myers May 21 '20 at 14:37
  • That isn't hard – James A Mohler May 21 '20 at 21:04
  • So, I am getting an error saying `VARIABLE JSONDATA IS UNDEFINED` I have to comment out my `cif arrayLen(jsonData)` that was wrapping over the cfscript – jonathon myers May 22 '20 at 13:48
  • However, it does not return certain key values that I wanted from the json. I dont want to return all the json key values. Can I just choose which key values I want to return? – jonathon myers May 22 '20 at 13:51
  • So, do you have a list of the values you want? Or perhaps the key name needs to have certain letters or perhaps only certain values? How do you know which keys you want? – James A Mohler May 22 '20 at 18:26
  • Basically, I want the list of the value I want.. Please see my other question where I am trouble converting from sql to cfscript. https://stackoverflow.com/questions/61959870/how-to-convert-from-sql-query-to-cfscript-properly-in-coldfusion?noredirect=1#comment109588733_61959870 – jonathon myers May 22 '20 at 18:28
  • I am also already return the certain one with `#prop.employeeId#`, ... and so on. see above code – jonathon myers May 22 '20 at 18:29