Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Using isdefined when json variable contains a #

Contributor ,
Dec 27, 2020 Dec 27, 2020

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.pngexpand 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

656
Translate
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

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

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

Dave Watts, Eidolon LLC
Translate
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

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
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
Community Expert ,
Dec 29, 2020 Dec 29, 2020
LATEST

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

Translate
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

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
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
Community Expert ,
Dec 28, 2020 Dec 28, 2020

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
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
Community Expert ,
Dec 27, 2020 Dec 27, 2020
<cfif structKeyExists(results.track.album.image[2], "##text") AND results.track.album.image[2]["##text"] NEQ ''>
	
</cfif>
Translate
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
Community Expert ,
Dec 27, 2020 Dec 27, 2020
<cfif not isNull(results.track.album.image[2]["##text"]) AND results.track.album.image[2]["##text"] NEQ ''>

</cfif>
Translate
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
Resources