how to stop CF returning booleans as YES / NO ?
# cfml-general
a
how to stop CF returning booleans as YES / NO ?
j
If you need it to return a boolean, you can use booleanFormat()
Copy code
booleanFormat( value )
Great to use if your values are also numeric and you want to represent any value other than 0 as
true
a
not great if you don't know the format/type of the variable
j
If you are getting a yes or no, which is your question it will always work
Unless your version of CF does not have that function. Another option is you use a Ternary conditional operator
Copy code
{your condition} ? true : false;
🙏 1
👍 1
For instance, you can do
Copy code
value EQ "yes" ? true : false;
a
ta
there's no setting in CFIDE presumably?
should've been more specific. This is happening on deserializejson
image.png
s
You can also test the variable with IsBoolean() if you’re not sure it’s always going to have a value that can be converted to true/false
a
I mean I've got a way around it for now. But in my screenshot, if I were to do
IsBoolean(name)
which would return false, I wouldn't want false returned
just CF being stupid converting true / false to yes no
s
Yeah, cf marching to the beat of its own drummer there. You would only need the isboolean or the ternary operator if it’s possible for values other than true/false might be in the json. otherwise you can just convert it with BooleanFormat(value)
d
I wonder if that's really a writeDump() artifact. Also, in most situations, for better and worse, cf thinks "yes" == true, so depending on what you're actually going to do, it may not matter.
j
I agree @Alan, that is an issue with CF’s
deserializeJSON
function. In the end, when working with JSON, you should have a general idea of what you are receiving, expecting and/or supporting, so you build it into your logic. The extra step to make sure yes/no to true false is a pain and really might only be necessary based on what you want to do with that data. As @Dave Merrill stated CF treats yes/no as true/false, so if you are using this to update a DB record and you are passing the value as a query param for a bit field, or as an argument to a function expecting a boolean, it will work without an issue.
d
OTOH, if you're exporting to a non-cf system, you probably do need to fix this up, either in the export or the import.
a
Thanks for the feedback / assistance all
m
Putting this in as an ER, can I get some upvotes?
2
😂 1