StructReduce conditional help

Okay, still learning the cfscripting conditionals & referencing variables in a struct. What am I doing wrong in my DOES NOT CONTAIN double conditional? This always returns zero for tcount because it never is considered “true”:

NonZeroCounts = StructReduce(TheJSON.data.counts, function(result, key, value) {
	if (value > 0) {
	    result[key] = value;
			if (result[key] DOES NOT CONTAIN "warning" && result[key] DOES NOT CONTAIN "error") {
				tcount = tcount + value;
			}
	}
	return result;
}, {}) ;

Appreciate any insight.

Dan

is tcount initialized outside of structReduced?

what does TheJSON.data.counts contain?

would returning a struct with two keys (data, tcount) help?
https://trycf.com/gist/e38ea3075468fca05b1ad58f4c31810f/lucee5?theme=monokai

Thanks for you response webonix…

It contains:

which was the Struct you’d helped me with prior.

Yes, tcount is initialized as zero before this (prior to the loop).

Since tcount is for Total Count at the end, I don’t think I need two keys. I just want the total but NOT adding entries if they are “warning” or “error” numbers.

Can you post the json struct please?

Sure, thanks! Here’s the whole raw JSON:

{"id":499,"created_at":"2022-06-29T23:02:07Z","started_at":"2022-06-29T23:03:47Z","ended_at":"2022-06-29T23:24:06Z","updated_at":"2022-06-29T23:24:07Z","progress":100,"workflow_state":"imported_with_messages","data":{"import_type":"instructure_csv","downloadable_attachment_ids":[1293161,1293162,1293163,1293164,1293165,1293166,1293167,1293168],"supplied_batches":["course","section","xlist","user","enrollment"],"counts":{"change_sis_ids":0,"accounts":0,"terms":0,"abstract_courses":0,"courses":3643,"sections":3643,"xlists":89,"users":66109,"logins":0,"enrollments":66975,"admins":0,"group_categories":0,"groups":0,"group_memberships":0,"grade_publishing_results":0,"user_observers":0,"error_count":0,"warning_count":27},"running_immediately":false,"completed_importers":["course","section","xlist","user","enrollment"],"statistics":{"total_state_changes":121,"Account":{"created":0,"restored":0,"deleted":0},"EnrollmentTerm":{"created":0,"restored":0,"deleted":0},"AbstractCourse":{"created":0,"restored":0,"deleted":0},"Course":{"created":1,"restored":0,"concluded":0,"deleted":0},"CourseSection":{"created":0,"restored":0,"deleted":0},"GroupCategory":{"created":0,"restored":0,"deleted":0},"Group":{"created":0,"restored":0,"deleted":0},"Pseudonym":{"created":1,"restored":0,"deleted":0},"CommunicationChannel":{"created":1,"restored":12,"deleted":0},"Enrollment":{"created":4,"restored":0,"concluded":0,"deactivated":102,"deleted":0},"GroupMembership":{"created":0,"restored":0,"deleted":0},"UserObserver":{"created":0,"restored":0,"deleted":0},"AccountUser":{"created":0,"restored":0,"deleted":0}}},"batch_mode":null,"batch_mode_term_id":null,"multi_term_batch_mode":null,"override_sis_stickiness":null,"add_sis_stickiness":null,"update_sis_id_if_login_claimed":false,"clear_sis_stickiness":null,"diffing_data_set_identifier":null,"diffing_remaster":null,"diffed_against_import_id":null,"diffing_drop_status":null,"skip_deletes":false,"change_threshold":null,"diff_row_count_threshold":null,"processing_warnings":[["users.csv","An existing Canvas user with the SIS ID 013239436 has already claimed 013239592's user_id requested login information, skipping"],["assignments.csv","User not found for enrollment (User ID: 150008658, Course ID: , Section ID: 2229-POSC-343-01-2889-1)"],["assignments.csv","User not found for enrollment (User ID: 150008658, Course ID: , Section ID: 2229-POSC-345-01-2891-1)"],["assignments.csv","User not found for enrollment (User ID: 150008658, Course ID: , Section ID: 2229-POSC-380-01-2896-1)"],["assignments.csv","User not found for enrollment (User ID: 004616192, Course ID: , Section ID: 2229-TED-474-01-1190-1)"],["assignments.csv","User not found for enrollment (User ID: 150008255, Course ID: , Section ID: 2229-ENVT-330-01-2299-1)"],["assignments.csv","User not found for enrollment (User ID: 150008255, Course ID: , Section ID: 2229-GEOG-120-01-2302-1)"],["assignments.csv","User not found for enrollment (User ID: 150008255, Course ID: , Section ID: 2229-GEOG-455-01-2300-1)"],["assignments.csv","User not found for enrollment (User ID: 150008268, Course ID: , Section ID: 2229-HIST-201-01-1656-1)"],["assignments.csv","User not found for enrollment (User ID: 150008268, Course ID: , Section ID: 2229-HIST-405-01-1674-1)"],["assignments.csv","User not found for enrollment (User ID: 150008268, Course ID: , Section ID: 2229-HIST-600-01-1684-1)"],["enrollments2229.csv","User not found for enrollment (User ID: 201567670, Course ID: , Section ID: 2229-EXTX-560-01HB-3801-DY1)"],["enrollments2227.csv","User not found for enrollment (User ID: 201365897, Course ID: , Section ID: 2227-ACCT-802C-03JV-1482-SSD)"],["enrollments2227.csv","User not found for enrollment (User ID: 201559376, Course ID: , Section ID: 2227-KIN-850-01HA-1737-DY1)"],["enrollments2227.csv","User not found for enrollment (User ID: 201559376, Course ID: , Section ID: 2227-KIN-851-01HA-1738-DY1)"],["enrollments2227.csv","User not found for enrollment (User ID: 201559376, Course ID: , Section ID: 2227-KIN-853-01HA-1740-DY1)"],["enrollments2227.csv","User not found for enrollment (User ID: 201559376, Course ID: , Section ID: 2227-KIN-854-01HA-1741-DY1)"],["enrollments2227.csv","User not found for enrollment (User ID: 201559376, Course ID: , Section ID: 2227-KIN-856-01HA-1743-DY1)"],["enrollments2227.csv","User not found for enrollment (User ID: 201567683, Course ID: , Section ID: 2227-TED-802C-01HA-1809-DY1)"],["enrollments2227.csv","User not found for enrollment (User ID: 201567683, Course ID: , Section ID: 2227-TED-841C-01HA-1810-DY1)"],["enrollments2227.csv","User not found for enrollment (User ID: 201567683, Course ID: , Section ID: 2227-TED-842C-1HA-1811-DY1)"],["enrollments2227.csv","User not found for enrollment (User ID: 201567683, Course ID: , Section ID: 2227-TED-843C-01HA-1812-DY1)"],["enrollments2227.csv","User not found for enrollment (User ID: 201567696, Course ID: , Section ID: 2227-TED-843C-01HA-1812-DY1)"],["enrollments2227.csv","User not found for enrollment (User ID: 201567683, Course ID: , Section ID: 2227-TED-844C-01HA-1814-DY1)"],["and 3 more warnings that were not included","Download the error file to see all warnings."]],"errors_attachment":{"id":1293294,"uuid":"O12B4oNCToE6o5Yt4qAF9QhEyhYlKu7t2n5h78Gq","folder_id":null,"display_name":"sis_errors_attachment_499.csv","filename":"211450000000000499_processing_warnings_and_errors2022-06-29+232407+UTC20220629-10640-1dq5pso.csv","upload_status":"success","content-type":"csv","url":"site","size":3638,"created_at":"2022-06-29T23:24:07Z","updated_at":"2022-06-29T23:24:07Z","unlock_at":null,"locked":false,"hidden":false,"lock_at":null,"hidden_for_user":false,"thumbnail_url":null,"modified_at":"2022-06-29T23:24:07Z","mime_class":"file","media_entry_id":null,"locked_for_user":false},"user":{"id":175,"name":"Dan Legate","created_at":"2022-01-19T13:21:38-08:00","sortable_name":"Legate, Dan","short_name":"Dan Legate","sis_user_id":"000022603","integration_id":null,"sis_import_id":499,"login_id":"dx7668"},"csv_attachments":[{"id":1293168,"uuid":"qgoQmze0dSP8yPj2MddkXtUTATQFSpiY1PQ7oBoX","folder_id":null,"display_name":"courses2229.csv","filename":"courses2229.csv","upload_status":"success","content-type":"text/csv","url":"site","size":383165,"created_at":"2022-06-29T23:03:50Z","updated_at":"2022-06-29T23:03:50Z","unlock_at":null,"locked":false,"hidden":false,"lock_at":null,"hidden_for_user":false,"thumbnail_url":null,"modified_at":"2022-06-29T23:03:50Z","mime_class":"file","media_entry_id":null,"locked_for_user":false},{"id":1293166,"uuid":"tXfDl8Gb8NeLiwE03QlrUmxnCtnBKu5vAdDj2WU7","folder_id":null,"display_name":"courses2227.csv","filename":"courses2227.csv","upload_status":"success","content-type":"text/csv","url":"site","size":112425,"created_at":"2022-06-29T23:03:49Z","updated_at":"2022-06-29T23:03:49Z","unlock_at":null,"locked":false,"hidden":false,"lock_at":null,"hidden_for_user":false,"thumbnail_url":null,"modified_at":"2022-06-29T23:03:49Z","mime_class":"file","media_entry_id":null,"locked_for_user":false},{"id":1293167,"uuid":"EL30yHiz4VOFMktXc18isi7qBk4vh9DUYaUOKZcr","folder_id":null,"display_name":"enrollments2227.csv","filename":"enrollments2227.csv","upload_status":"success","content-type":"text/csv","url":"site","size":800855,"created_at":"2022-06-29T23:03:50Z","updated_at":"2022-06-29T23:03:50Z","unlock_at":null,"locked":false,"hidden":false,"lock_at":null,"hidden_for_user":false,"thumbnail_url":null,"modified_at":"2022-06-29T23:03:50Z","mime_class":"file","media_entry_id":null,"locked_for_user":false},{"id":1293162,"uuid":"NrV3dfBHX35YBZhw6NaPBHsFnfrS9ZZUk4JTevb1","folder_id":null,"display_name":"assignments.csv","filename":"assignments.csv","upload_status":"success","content-type":"text/csv","url":"site","size":192244,"created_at":"2022-06-29T23:03:48Z","updated_at":"2022-06-29T23:03:48Z","unlock_at":null,"locked":false,"hidden":false,"lock_at":null,"hidden_for_user":false,"thumbnail_url":null,"modified_at":"2022-06-29T23:03:48Z","mime_class":"file","media_entry_id":null,"locked_for_user":false},{"id":1293161,"uuid":"5gMV74wYat4jkz9cOlHYI4fATggxqH0PcGTb4vuy","folder_id":null,"display_name":"xlists.csv","filename":"xlists.csv","upload_status":"success","content-type":"text/csv","url":"site","size":5683,"created_at":"2022-06-29T23:03:48Z","updated_at":"2022-06-29T23:03:48Z","unlock_at":null,"locked":false,"hidden":false,"lock_at":null,"hidden_for_user":false,"thumbnail_url":null,"modified_at":"2022-06-29T23:03:48Z","mime_class":"file","media_entry_id":null,"locked_for_user":false},{"id":1293163,"uuid":"pOwJn08JzTWDaQDK3hsXCJtTUwHXfvOkDd7PMHfY","folder_id":null,"display_name":"users.csv","filename":"users.csv","upload_status":"success","content-type":"text/csv","url":"site","size":5788661,"created_at":"2022-06-29T23:03:49Z","updated_at":"2022-06-29T23:03:49Z","unlock_at":null,"locked":false,"hidden":false,"lock_at":null,"hidden_for_user":false,"thumbnail_url":null,"modified_at":"2022-06-29T23:03:49Z","mime_class":"file","media_entry_id":null,"locked_for_user":false},{"id":1293164,"uuid":"R3oTHwNYgXn0XV9tbQmO2flRZd35CiVk0SfnwdOp","folder_id":null,"display_name":"enrollments2229.csv","filename":"enrollments2229.csv","upload_status":"success","content-type":"text/csv","url":"site","size":3500440,"created_at":"2022-06-29T23:03:49Z","updated_at":"2022-06-29T23:03:49Z","unlock_at":null,"locked":false,"hidden":false,"lock_at":null,"hidden_for_user":false,"thumbnail_url":null,"modified_at":"2022-06-29T23:03:49Z","mime_class":"file","media_entry_id":null,"locked_for_user":false},{"id":1293165,"uuid":"jfKZmLH5AMJ4GshvnPM56rL9Bb4iDKG9xaA8DnJM","folder_id":null,"display_name":"sections.csv","filename":"sections.csv","upload_status":"success","content-type":"text/csv","url":"site","size":383991,"created_at":"2022-06-29T23:03:49Z","updated_at":"2022-06-29T23:03:49Z","unlock_at":null,"locked":false,"hidden":false,"lock_at":null,"hidden_for_user":false,"thumbnail_url":null,"modified_at":"2022-06-29T23:03:49Z","mime_class":"file","media_entry_id":null,"locked_for_user":false}]}

Let me know if you’d rather have it formatted. :slight_smile:

I’d also love to learn how to loop over and grab these variables (struct inside an array which is inside a parent struct)

luceedev_array_struct

There are multiple items in that array, as you’ll see if you dump it too.

The struct/array nesting that occurs from a deserialized JSON dump can be crazy! haha

Dan

minor tweak to contains line

if (key DOES NOT CONTAIN “warning” && key DOES NOT CONTAIN “error”) {

https://trycf.com/gist/c1d9c0de2358e1251c89aef4d41017fa/lucee5?theme=monokai

image.png

Brilliant… such a simple change.

Thank you SO much!

Any tips on getting that “display_name” variable inside the struct/array/struct?

opps forgot the most importance line
https://trycf.com/gist/9cc2aa24fd6a58238d1cfe019d3fb1da/lucee5?theme=monokai

NonZeroCounts = StructReduce(TheJSON.data.counts, function(result, key, value) {
if (value > 0) {
result[‘data’][key] = value;
if (key DOES NOT CONTAIN “warning” && key DOES NOT CONTAIN “error”) {
result[‘tcount2’] += value;
tcount += value;
}
result[‘tcount3’] += value;
}
return result;
}, {data:{},tcount2: 0, tcount3: 0}) ;

Actually your previous post worked perfectly too!

Still looking for how to grab display_name etc out of:

need some more details on what you are trying to achieve here
maybe sample result data would help

Oh sure!

From the full Raw JSON I sent earlier in the thread, I’m looking to be able to extract:

display_name: courses2229.csv
created_at: 2022-06-29T23:03:50Z

display_name: enrollments2227.csv
created_at: 2022-06-29T23:03:50Z

etc.

which appear to be nested inside a struct / array / struct.

Not sure how to “get at” or reference those.

Does that make sense?

try this
https://trycf.com/gist/8d59f280425ef5c3f1d4a522cf435b99/lucee5?theme=monokai

csv_attachments = ArrayReduce(
TheJSON.csv_attachments, function(result, key, value) {

result.append({
‘created_at’: key[‘created_at’],
‘display_name’: key[‘display_name’]
})

return result;
}, );

Tremendously helpful!

Thank you SO much!