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

How to open and read JSON file?

Enthusiast ,
Mar 07, 2016 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?

Views

13.1K

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
community guidelines

correct answers 1 Correct answer

Community Expert , Mar 07, 2016 Mar 07, 2016

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

Votes

Translate

Translate
Guide ,
Mar 07, 2016 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.

Votes

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
community guidelines
Enthusiast ,
Mar 07, 2016 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!

Votes

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
community guidelines
Enthusiast ,
Mar 07, 2016 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?

Votes

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
community guidelines
Guide ,
Mar 07, 2016 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.

Votes

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
community guidelines
Enthusiast ,
Mar 07, 2016 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.

Votes

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
community guidelines
Community Expert ,
Mar 07, 2016 Mar 07, 2016

Copy link to clipboard

Copied

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

Votes

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
community guidelines
Enthusiast ,
Mar 07, 2016 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?

Votes

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
community guidelines
Community Expert ,
Mar 07, 2016 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)

Votes

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
community guidelines
Enthusiast ,
Mar 07, 2016 Mar 07, 2016

Copy link to clipboard

Copied

Thanks, BKBK!

Votes

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
community guidelines
Enthusiast ,
Mar 10, 2016 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")) />

Votes

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
community guidelines
Enthusiast ,
Mar 10, 2016 Mar 10, 2016

Copy link to clipboard

Copied

LATEST

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)) />

Votes

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
community guidelines
Resources
Documentation