Using isdefined when json variable contains a #

Contributor ,
Dec 27, 2020 Dec 27, 2020

Copy link to clipboard

Copied

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:

Screen Shot 2020-12-27 at 2.32.42 PM.png

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

Views

166

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct Answer

Adobe Community Professional , Dec 27, 2020 Dec 27, 2020
<cfif structKeyExists(results.track.album.image[2], "##text") AND results.track.album.image[2]["##text"] NEQ ''> </cfif>

Likes

Translate

Translate
Adobe Community Professional ,
Dec 27, 2020 Dec 27, 2020

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 27, 2020 Dec 27, 2020

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Dec 29, 2020 Dec 29, 2020

Copy link to clipboard

Copied

LATEST

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 27, 2020 Dec 27, 2020

Copy link to clipboard

Copied

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) /> 

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Dec 28, 2020 Dec 28, 2020

Copy link to clipboard

Copied

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().

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Dec 27, 2020 Dec 27, 2020

Copy link to clipboard

Copied

<cfif structKeyExists(results.track.album.image[2], "##text") AND results.track.album.image[2]["##text"] NEQ ''>
	
</cfif>

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Dec 27, 2020 Dec 27, 2020

Copy link to clipboard

Copied

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

</cfif>

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines