I'm comparing values in a session variable that are in comma separated to local JSON values. What is the best way to read the comma separated value from the session.groups variable?
Copy link to clipboard
Show us some sample data and what you mean by "comparing".
The session variable contains values like this:
In the loop of each of the JSON data, I want to compare if the data in the json matches one or more of the data in the session variable.
How about a sample of the JSON data?
In general, comma-separated data stored in ColdFusion variables are treated as lists, so you can use any of the LISTxxx() functions on them. In your case, you can probably use ListFind() or ListFindNoCase() (the latter if the JSON data isn't in upper case like the sample data you showed above. Also, you can turn a list into an array using ListToArray(), then use Arrayxxx() functions to match your values. Arrays usually perform a bit better than lists if the lists are fairly long.
Thanks, Carl! That is very helpful. So the ListToArray() will convert the session variable of list into array then, correct?
Here's a sample of the jsson data.
"Group_Name":["KB Developer","BBL","Camp","BC Front-Line","KB Publishers"],
Basically, the each item in the group_name matches the group_id. I can arrange this however I want. I have this as an CSV file format originally. I'm still not sure if JSON array, JSON column array, or just JSON property is easier/faster when comparing to another array or list. I'm using Mr. Data Converter to convert my CSV to JSON data.
You're probably better off converting the data directly into ColdFusion structs/arrays rather than working in JSON. You can do that with DeserializeJSON().
I'm not seeing a 1:1 correlation of the group names in your JSON with the names stored in your session variable, so how are you planning on comparing them?
There are three things involved here. The article has a group_id that only certain groups can access it. So, I'm thinking that once the user click on the article, in the details-page, it will prompt for the user to authenticate. Once the user is authenticated, the session variable will store what group the user belongs to. Then test if the list of groups that the user belongs to exist in the JSON group_name array. If it does exist, then get the group_id of that group_name and see if the article's group_id that's being passed in the URL parameter matches. If it matches, then display the article, if not then display a message to the user.
That all sounds reasonable. Again though, based on the samples you provided, I don't see direct matches between the Group_Name values and any of the data in the session variable sample. If the samples are merely hypothetical, that's fine. But if that is actual data, I don't see any matches between them.
They're just hypothetical sample data only. Again, thank you for your help.
Copy link to clipboard
What is the best way to read the comma separated value from the session.groups variable?
The best way in Coldfusion to read a variable that consists of comma-separated values is to treat it as a list.