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

How to open and read JSON file?

Enthusiast ,
Mar 07, 2016 Mar 07, 2016

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

13.7K
Translate
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")>

Translate
Guide ,
Mar 07, 2016 Mar 07, 2016

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.

Translate
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

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

Thanks!

Translate
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

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?

Translate
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

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.

Translate
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

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.

Translate
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

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

Translate
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

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?

Translate
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

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)

Translate
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

Thanks, BKBK!

Translate
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

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

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

Translate
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