Could someone please explain how to get to the "at...
# cfml-beginners
e
Could someone please explain how to get to the "attributes" structure. I need to get to "carrier" and "tracking_number". Code below returns correct result
<cfset jsonData = deserializeJSON(httpResp.fileContent) />
<cfset return_line_items = jsonData.included>
<cfloop array=#return_line_items# index="i">
<cfset line_item_id = #i.id#>
<cfset line_item_type = #i.type#>
<cfoutput>
Line_item_id:   #line_item_id#<br />
Line_item_type: #line_item_type#<br />
</cfoutput>
<br />
</cfloop>
, but when I try to get to carrier this way, I am receiving the error: "Invalid collection .... Must be a valid structure or COM object."
<cfif #i.type# EQ "shipping_labels">
<cfloop collection="#jsonData.included#" item="k">
<cfset carrier = #jsonData.included.attributes.[k].carrier#>
<cfoutput>#carrier#<br /></cfoutput>
</cfloop>
</cfif>
a
BTW, you don't need to use
#
in your if statements. So this
<cfif #i.type# EQ "shipping_labels">
is the same as
<cfif i.type EQ "shipping_labels">
d
if you want to use the "collection" attribute of cfloop you should reference the "k" item instead.
a
To explain where your code breaks:
jsonData.included
<- this is an array
jsonData.included.attributes
<- this breaks as
included
is an array and you are trying to reference a key, rather than the index.
Hopefully the runnable code example I posted will make it clear.
d
ah yes, you can't use collection here. you're first code snippet that works is fine or the example provided by alias is good too.
e
Ahhh, thank you very much. It works. I am always confused when to use/ not to use #s
a
You generally use '#' when outputting things. If in doubt you can always ask 🙂 Splitting out logic from presentation makes this clearer (so if you can - use cfscript for logic and then tags for presentation (views)
e
Well, this opens up a whole new situation. I started to learn CF because it's a "tag" language. I don't think I can write a single line of <cfscript>. I was told that "true" CF devs write everything in <cfsctipt>, but I am not a web dev, but database developer. To me learning CF sctipting is like learning a whole new way to write code.
☝🏻 1
a
I get it - getting the job done is what pays the bills 🙂 but if you are interested in the code side of things then folks on here will help you which will open up other programming languages to you as well.
e
Thank you!
d
Looks like you're doing enough coldfusion dev work these days that learning script would be beneficial to you. Rendering data views only requires a few tags, but business logic requires a lot more lines of code and the amount of methods you could use in your business logic could be enormous. script will be much less verbose and easier to read <-- the obvious benefit .
j
I also learned CFML because it was tag based. If i wanted to script all day I would have stuck with PHP. I disagree scripting is easier to read. It's all how you organize it and make it work. But, ya, gotta pay those bills…