I need to parse Json file in CF2018. I copied json...
# cfml-beginners
e
I need to parse Json file in CF2018. I copied json data into returnly_json.txt file and placed it into c:\temp directory on the server. When I try
<cfif isJSON(\\server\c$\Temp\returnly_json.txt)>
, I am getting this error:
Invalid CFML construct found on line 85 at column 18.
ColdFusion was looking at the following text:
//
. What am I doing wrong?
m
It's a security string you'll need to get rid of before checking for/parsing JSON.
e
I changed file name to return.json and added double quotes. I can open it in Sublime, for instance w/out any issues. Now,
<cfif isJSON("\\server\c$\Temp\return.json")>
saying it's not a valid Json format
m
Does it still have the "\\" at the front?
e
The json file snippet:
{
"data": [
{
"type": "returns",
"id": "327031",
"attributes": {
"rma": "327031",
"ext_store_id": null,
m
Why did you add double quotes? where did you add double quotes?
s
isjson parses a string to see if it's json... it kind of looks like you are entering a file path. the isjson function will not be able to read your file. you will need to do a file read first to get the contents of the file.
👍 1
somethign like this:
Copy code
<cfscript>
isJson(FileRead("\\server\c$\Temp\return.json");
</cfscript>
or if you need to do other stuff with the file contents you might want to set a variable like:
Copy code
<cfscript>
myfile = FileRead("\\server\c$\Temp\return.json");
if (isJSON(myfile)){
//do some stuff with the json
}
</cfscript>
e
This does not work
<cfscript>
myfile = FileRead("\\server\c$\Temp\return.json");
</cfscript>
<cfif isJSON(#myfile#)>
<cfset jsonData = deserializeJSON(#myfile#) />
<cfdump var="#jsonData#"><!--- <cfabort> --->
</cfif>
m
what's the error? "does not work" isn't useful for debugging.
e
Not valid json format
m
how was the JSON generated? can you check it with a linter?
e
m
Your snippet above doesn't strip out the leading "\\"
e
removed them, but still the same issue
m
Part two of my question from above: can you check it with a linter??
e
I will have to find out how to do it, working on it ...
s
if you are using tag based you might want to just use cffile
Copy code
<cffile action= "Read" file="\\server\c$\Temp\return.json" variable="myfile">
   
    <cfif isJSON(#myfile#)>
        <cfset jsonData = deserializeJSON(#myfile#) />			
         <cfdump var="#jsonData#"><!--- <cfabort> --->
	</cfif>
also, it looks like your json is invlid
(I pasted it into https://jsonparser.org/ and it looksl ike there are some errors at the bottom on line 255 even after you strip out the //
e
Awwww... silly me, the Json was missing "}" at the end
Thank you both for your help
s
no problem