How to return in struct with my cfoutput?

182 Views Asked by At

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",
}
]
1

There are 1 best solutions below

9
James A Mohler On

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>