Highlighted

What is the best way to read value from a session variable that has comma separated values?

Engaged ,
Feb 29, 2016

Copy link to clipboard

Copied

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?

Views

295

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

What is the best way to read value from a session variable that has comma separated values?

Engaged ,
Feb 29, 2016

Copy link to clipboard

Copied

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?

Views

296

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
Feb 29, 2016 0
Most Valuable Participant ,
Mar 01, 2016

Copy link to clipboard

Copied

Show us some sample data and what you mean by "comparing".

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
Reply
Loading...
Mar 01, 2016 0
Engaged ,
Mar 01, 2016

Copy link to clipboard

Copied

The session variable contains values like this:

KB.DEVUNITS.BUSINESS-COMMUNICATION,KB.DEVUNITS.ACCOUTING-FINANCE,KB.DEVUNITS.BIOLOGY,KB.FILES.ADMIN,KB.DEVUNITS.NURSING,KB.DEVUNITS.ARMY-ROTC,KB.DEVUNITS.MANAGEMENT-MARKETING,KB.DEVUNITS.ADMISSIONS,KB.DEVTRAINING,KB.DEVUNITS.HOUSING-RESIDENCE-LIFE,KB.DEVUNITS.ARTS-AND-SCIENCE,KB.DEVUNITS.ON-CAMPKB-MBA,KB.DEVUNITS.ATHLETICS,KB.DEVUNITS.INFORMATION-SYSTEMS,KB.DEVUNITS.COLLEGE-OF-BUSINESS,KB.DEVUNITS.STUDENT-SENATE,KB.DEVUNITS.CAMPKB-LIFE,KB.DEVUNITS.POWER-OF-AND,KB.DEVUNITS.CHAASE,KB.DEVDEVELOPER,KB.DEVUNITS.NEWS,KB.CSADMIN,KB.DEVUNITS.CADE,KB.DEVUNITS.CONTINUING-ED,KB.DEVUNITS.COLLEGE-OF-EDUCATION-AND-HUMAN-SCIENCES,KB.DEVAUTHOR.ACCESS,KB.DEVUNITS.KOKB,KB.DEVUNITS.SALES-CENTER,KB.DEVUNITS.LANGUAGES,KB.CSDEVELOPER,KB.DEVUNITS.NURSING-HEALTH-SCIENCES,KB.KBREDESIGN,KB.DEVUNITS.SPECIAL-EDUCATION,KB.DEVUNITS.ON-CAMPKB-DINING,KB.DEVUNITS.RECREATION-INTRAMURALS,KB.DEVUNITS.OFFICE-OF-INTERDISCIPLINARY-PROGRAMS,KB.DEVUNITS.PSYCH,KB.DEVADMIN,KB.DEVUNITS.ACADEMICS

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.

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
Reply
Loading...
Mar 01, 2016 0
Most Valuable Participant ,
Mar 01, 2016

Copy link to clipboard

Copied

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.

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
Reply
Loading...
Mar 01, 2016 0
Engaged ,
Mar 01, 2016

Copy link to clipboard

Copied

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"],

   "Group_ID":["56c4cfe091231c0b5b47fe66","563a13bf31231cf46f5d1db8","563a13c631231c8a6f5d1dc1","56a13f7c32123c7f6831a372","55e722df31231cf85008b580"]

}

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.

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
Reply
Loading...
Mar 01, 2016 0
Most Valuable Participant ,
Mar 01, 2016

Copy link to clipboard

Copied

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?

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
Reply
Loading...
Mar 01, 2016 0
Engaged ,
Mar 01, 2016

Copy link to clipboard

Copied

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.

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
Reply
Loading...
Mar 01, 2016 0
Most Valuable Participant ,
Mar 01, 2016

Copy link to clipboard

Copied

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.

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
Reply
Loading...
Mar 01, 2016 0
Engaged ,
Mar 01, 2016

Copy link to clipboard

Copied

They're just hypothetical sample data only. Again, thank you for your help.

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
Reply
Loading...
Mar 01, 2016 0
BKBK LATEST
Adobe Community Professional ,
Mar 05, 2016

Copy link to clipboard

Copied

2Charlie wrote:

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.

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
Reply
Loading...
Mar 05, 2016 0