Copy link to clipboard
Copied
Hello,
I need to find a value within an array. In the code example below I need to find ID when Country=US. In this code example, the result I need is found at results2.releases[5].ID however the order and number of results in the array vary depending on the JSON returned. How do I loop over the results and find the ID when country = US?
As always appreciate any help!
Gary
<cfhttp method="get" url="http://musicbrainz.org/ws/2/recording/729cf505-94eb-4fbe-bc76-cbae44cff091?inc=artist-credits+isrcs+releases&fmt=json" result="httpResults2" throwonerror="Yes">
</cfhttp>
<cfset results2 = deserializeJSON(httpResults2.filecontent)>
<cfdump var="#results2#" />
You could use ArrayFilter
<cfscript>
releasesArray=results2.releases;
if ( arrayLen(releasesArray) gt 0 ) {
arrayOfUSReleases=arrayFilter(releasesArray,function(item){
return item.country eq 'US';
});
/* writedump(arrayOfUSReleases);*/
/* Get the IDs. Note that each element of arrayOfUSReleases is a struct. */
arrayofUSReleaseIDs=arrayNew(1);
for (index=1; index lte arrayLen(arrayOfUSReleases); index=index+1) {
arrayAppend(arrayofUSReleaseIDs,arrayOfUSR
...
Copy link to clipboard
Copied
You could use ArrayFilter
<cfscript>
releasesArray=results2.releases;
if ( arrayLen(releasesArray) gt 0 ) {
arrayOfUSReleases=arrayFilter(releasesArray,function(item){
return item.country eq 'US';
});
/* writedump(arrayOfUSReleases);*/
/* Get the IDs. Note that each element of arrayOfUSReleases is a struct. */
arrayofUSReleaseIDs=arrayNew(1);
for (index=1; index lte arrayLen(arrayOfUSReleases); index=index+1) {
arrayAppend(arrayofUSReleaseIDs,arrayOfUSReleases[index].id);
}
writedump(arrayofUSReleaseIDs) ;
}
</cfscript>
Copy link to clipboard
Copied
Thanks! The code work and I appreciate you commenting it out as you went along. I have zero experience writing scripts so your /comments/ at least allowed me to somewhat follow along. It's on my bucket list of things to learn.
Gary
Copy link to clipboard
Copied
One more quick question...
I've tried all different [ ] placements to get CF not to see the dash in label-info as an operative. I've found a bunch of links when I search for help that refers to a CF8 document that now returns a 404. How do I get name out of:
<cfset label = #results3.[label-info][1].label.name# /> --- my last attempt.
Copy link to clipboard
Copied
Use quotes.
<cfset myStruct={"label-info"=[{"catalog-number"="undefined",label={disambiguation="aka Parlophone UK","sort-name"="Parlophone"}}]}>
<cfoutput>
Catalog Number: #myStruct["label-info"][1]["catalog-number"]# <br>
Sort name: #myStruct["label-info"][1]["label"]["sort-name"]#
</cfoutput>
Copy link to clipboard
Copied
Following your example above I tried:
<strong>Label:</strong> #results3.["label-info"][1]["label"]["name"]#<br>
and got an error. Suggestions?
A CFML variable name cannot end with a "." character. |
The variable results3. ends with a "." character. You must either provide an additional structure key or delete the "." character. |
Copy link to clipboard
Copied
You didn't follow my example.
Mine: myStruct[]
Yours: results3.[]
Copy link to clipboard
Copied
ghanna, it's not results3.[ but just results3[, as BKBK showed with his reference to myStruct[. A subtle but critical difference.
And if somehow that's "not it" alone, I would recommend that you would do better to try doing a CFDUMP (or writedump, for cfscript) of the various bits of that variable, to find where any error is. So try first:
<cfdump var="#results3#">
then:
<cfdump var="#results3.["label-info"]#">
then:
<cfdump var="#results3.["label-info"][1]#">
and so oneach time doing a cfabort after it, to see what it shows. (Or you could go in the reverse direction, taking things OFF from the right until it "works".)
Either way, you will find whatever else is amiss. I'm offering this as a general suggestion in the interest of both maybe helping you and others reading along, or beyond this one need. 🙂
Hope the first suggestion was the answer you needed.
Copy link to clipboard
Copied
Doh, bkbk, our replies show being literally 2 seconds apart. That's a first--and you won the race!
Copy link to clipboard
Copied
Thanks, Gentlemen! Those damn pesky periods and commas get me all the time. This particular project long term will update a database for us by pulling data from 3 different online music databases using their APIs but for now, I threw it up on my personal site and added a search form to it just for fun. Play around if you got a minute. garyhanna.me/musictrack.cfm - I still need to catch errors if they API's don't return info though.
Again as always, I appreciate your help Charlie and BKBK!
Gary
Copy link to clipboard
Copied
Please mark the correct answer. You may of course mark your own explanation as the correct answer. 🙂
The key thing is that correct answers will help others by narrowing their search.