Highlighted

How to open and read JSON file?

Engaged ,
Mar 07, 2016

Copy link to clipboard

Copied

Is there a specific way to open and read a json file other than using cfhttp tag?

Adobe Community Professional
Correct answer by BKBK | Adobe Community Professional

<cfset koGroupsJSON = fileRead("https://devbox.mysite.com/search/KOGroups.json")>

Views

5.9K

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

How to open and read JSON file?

Engaged ,
Mar 07, 2016

Copy link to clipboard

Copied

Is there a specific way to open and read a json file other than using cfhttp tag?

Adobe Community Professional
Correct answer by BKBK | Adobe Community Professional

<cfset koGroupsJSON = fileRead("https://devbox.mysite.com/search/KOGroups.json")>

Views

5.9K

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
Mar 07, 2016 0
Most Valuable Participant ,
Mar 07, 2016

Copy link to clipboard

Copied

Depends on where the file is located.  If you are pulling it from a remote site, then CFTTP seems reasonable.  If it's on a local drive or a network drive, then CFFILE should work.  And keep in mind there isn't anything special about a "JSON" file.  It's really just a text file, albeit in JavaScript Object Notation format in terms of structure.

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 07, 2016 0
Engaged ,
Mar 07, 2016

Copy link to clipboard

Copied

It's a local file. I was afraid that cfhttp would not be as safe as cffile.

Thanks!

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 07, 2016 0
Engaged ,
Mar 07, 2016

Copy link to clipboard

Copied

Okay, so this is what I have in cftthp: <cfhttp url="https://devbox.mysite.com/search/KOGroups.json" method="get" timeout="15" />

However, when I try the fileread I have this:

<cfset kogroups = deserializeJSON(fileread(#KOGroups.json#)) />

My question now is, how do specify the path in the fileread above?

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 07, 2016 0
Most Valuable Participant ,
Mar 07, 2016

Copy link to clipboard

Copied

Again, if it's a local file, why are you using CFHTTP?  Just use CFFILE with the "read" action to read the contents into a variable, then use deserializeJSON on that 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 07, 2016 0
Engaged ,
Mar 07, 2016

Copy link to clipboard

Copied

As I've said above, I tried to use the <cfset kogroups = deserializeJSON(fileread(#KOGroups.json#)) /> but I'm not sure how to specify the path. It's unable to find the file. The actual location of the KOGroups.json is located in a shared folder and not on the IIS server. The path is "https://devbox.mysite.com/search/KOGroups.json but I'm not sure how to specify a relative path instead of a absolute path.

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 07, 2016 0
Adobe Community Professional ,
Mar 07, 2016

Copy link to clipboard

Copied

<cfset koGroupsJSON = fileRead("https://devbox.mysite.com/search/KOGroups.json")>

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 07, 2016 0
Engaged ,
Mar 07, 2016

Copy link to clipboard

Copied

Okay, that works and many thanks for that but since both cffile and cfhttp use the "https" path, is there any security advantage of one over the other?

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 07, 2016 0
Adobe Community Professional ,
Mar 07, 2016

Copy link to clipboard

Copied

That question only has theoretical interest. Cffile and cfhttp are making the calls within Coldfusion, so they are both secure. (In the sense that, in either case, an outsider will be unable to access the 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 07, 2016 1
Engaged ,
Mar 07, 2016

Copy link to clipboard

Copied

Thanks, BKBK!

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 07, 2016 0
Engaged ,
Mar 10, 2016

Copy link to clipboard

Copied

Okay, something odd happened. It's now giving me this error:

Error in custom script module

(/devsite/customcf/knowledge/articleDetail.cfm)

An error occurred when performing a file operation exists on file /search/KOGroups.json.

The cause of this exception was: org.apache.commons.vfs2.FileSystemException: Could not determine the type of file "https://devbox.mysite.com/search/KOGroups.json"

This is my code:

<cfset groupData = deserializeJSON(fileread("https://devbox.mysite.com/search/KOGroups.json")) />

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 10, 2016 0
2Charlie LATEST
Engaged ,
Mar 10, 2016

Copy link to clipboard

Copied

Okay, some help from my other thread, I found that using expandPath('fileName') will generate the absolute path and that seems to work. So for my future reference, this is the code that works:

<cfset groupData = deserializeJSON(fileread(\\commonspotshare.mysite.com\commonspot$\DEVELOPMENT\devbox.mysite.com\kb\article\KOGroups.json)) />

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 10, 2016 0