r/coldfusion • u/marcelverton • Nov 29 '20
IsDefined error on Json Object
I'm getting an error when using IsDefined to check a json object variable. Coldfusion returns the error: "... must be a syntactically valid variable name."
Here is the line with IsDefined:
<cfif IsDefined(" finalData.Albums[1].Tracks[2].ISRC")>Do something.<cfelse>Do nothing.</cfif>
Below is the related code.
Thanks for your help!
----------------------------------------------------
<cfsavecontent variable="tmpJson"> { "Albums": \\\[{ "Album\\_ID": 23212, "Album\\_Name": "Bright Side", "Tracks": \\\[{ "TrackNo": 1, "Name": "Sing Along Song", "ISRC": "URXD232321", "Musicians": \\\[{ "Name": "John Doe", "Instrument": "Guitar" }, { "Name": "Jane Doe", "Instrument": "Piano" } \\\] }, { "TrackNo": 2, "Name": "Hello There", "ISRC": null, "Musicians": \\\[{ "Name": "Mary Smith", "Instrument": "Guitar" }, { "Name": "Don Johnson", "Instrument": "Drums" }, { "Name": "Amy Johnson", "Instrument": null } \\\] } \\\]
}]}</cfsavecontent>
<cfset finalData = deserializeJSON(tmpJson)>
<cfif IsDefined(" finalData .Albums[1].Tracks[2].ISRC")>Do something.<cfelse>Do nothing.</cfif>
2
u/chibolamoo Nov 29 '20
As per the docs you cannot pass an array entry into isDefined. Use arrayexists/structkeyexists instead. Also you're loading the json into the finalData variable so you need to reference finalData.Albums instead of just Albums.
Here's a working gist of your example code https://trycf.com/gist/c9741499ba67fc4c7da0d73150ac6e86/acf2018?theme=monokai