Could someone please explain how to get to the "at...
# cfml-beginners
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 =>
<cfset line_item_type = #i.type#>
Line_item_id:   #line_item_id#<br />
Line_item_type: #line_item_type#<br />
<br />
, 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>
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">
if you want to use the "collection" attribute of cfloop you should reference the "k" item instead.
To explain where your code breaks:
<- this is an array
<- this breaks as
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.
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.
Ahhh, thank you very much. It works. I am always confused when to use/ not to use #s
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)
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
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.
Thank you!
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 .
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…