Skip to main content
Inspiring
Dec 27, 2020 7:42 PM UTC
Answered

Using isdefined when json variable contains a #

Hello,

I need help understanding how to check if a variable is defined when the json variable contains a # in its name as shown in the image:

I tried using ## but it not correct:

<cfif Isdefined('results.track.album.image[2].##text') AND #results.track.album.image[2].##text# NEQ ''>

 Suggestions?

 

Thanks!
Gary

    This topic is closed to new replies. Start a new post to keep the conversation going.
    BKBK
    BKBKCorrect answer
    Community Expert
    Dec 27, 2020 9:23 PM UTC
    <cfif structKeyExists(results.track.album.image[2], "##text") AND results.track.album.image[2]["##text"] NEQ ''>
    	
    </cfif>

    7 replies

    BKBK
    Community Expert
    BKBKCommunity ExpertCorrect answer
    Community Expert
    Dec 27, 2020 9:23 PM UTC
    <cfif structKeyExists(results.track.album.image[2], "##text") AND results.track.album.image[2]["##text"] NEQ ''>
    	
    </cfif>
    Translate
    BKBK
    Community Expert
    Community Expert
    Dec 27, 2020 9:36 PM UTC
    <cfif not isNull(results.track.album.image[2]["##text"]) AND results.track.album.image[2]["##text"] NEQ ''>
    
    </cfif>
    
    Translate
    ghanna1Author
    Inspiring
    Dec 27, 2020 9:20 PM UTC

    Might not be the best way to do it but this worked to simply remove the # for the JSON results:

    <cfset content = replace(#cfhttp.filecontent#, "##", "", "all") />
    <cfset results = deserializeJSON(content) /> 

     

    Translate
    BKBK
    Community Expert
    Community Expert
    Dec 28, 2020 10:33 AM UTC

    ghanna1

    There is no need to apply replace() beforehand. You got an error because isDefined() cannot be used in this case.

     

    To use isDefined() the argument has to be a valid variable-name. However, results.track.album.image[2]['##text'] is not a valid variable-name. 

     

    To test for existence in this case, the appropriate methods to use are, for example, isNull() or structKeyExists().

    Translate
    Community Expert
    Dec 27, 2020 8:13 PM UTC

    I think you'll have to treat it like an associative array. This is how you deal with any structure members whose names don't correspond to CF variable naming rules.

     

    results.track.album.image[2]['#text']

     

    Dave Watts, Eidolon LLC

    Translate
    Dave Watts, Eidolon LLC
    ghanna1Author
    Inspiring
    Dec 27, 2020 8:44 PM UTC

    I tried your suggestion:

    <cfif Isdefined("results.track.album.image[2]['#text']")>
    do something
    </cfif>

    and still got:

    • An expression that began on line 26, column 48.
      The expression might be missing an ending #, for example, #expr instead of #expr#.
    • An expression beginning with /", on line 26, column 17.This message is usually caused by a problem in the expressions structure.
    • An expression beginning with Isdefined, on line 26, column 7.This message is usually caused by a problem in the expressions structure.
    • A cfif tag beginning on line 26, column 2.
    Translate
    BKBK
    Community Expert
    Community Expert
    Dec 29, 2020 1:15 PM UTC

    Please confirm whether or not you're satisfied with the answers.

    Translate