1
votes

I am attempting to parse Coldfusion JSON data to make it look "normal" ColdFusion json:

{"ROWCOUNT":3,"COLUMNS":["ROWID","REL","DATE","FOA","TITLE","APPRECEIPE","OPENING","KEYWORDS","DOC","PURPOSE","APP","NAME","PURPOSE"],"DATA":{"ROWID":[24842,24841,23780],"REL":["032","031","108"],...

Searched on the web and found this link:

http://www.tysoncadenhead.com/blog/parsing-coldfusion-json-on-the-client-side#.V-UpKfkrKUk

He offers to use this function:

Object.prototype.parseCFJSON = function() {
var result = [],
   data = this;
for (var j = 0; j < data.DATA.length; j++) {
  result[j] = {};
  for (var i = 0; i < data.COLUMNS.length; i++) {
     result[j][data.COLUMNS[i].toLowerCase()] = data.DATA[j][i];
  }
}
return result;
};

Here is my code

<cfset jsonURL = SerializeJSON(SmartGuideSearchRet,true)>
<cfset URLd = #SmartGuideSearchRet#> //My CF Struct
<script>
<cfoutput>
var #toScript(URLd,"URLd")#;
</cfoutput>
Object.prototype.parseCFJSON = function() {

var result = [],
   data = this;

for (var j = 0; j < data.DATA.length; j++) {
  result[j] = {};
  for (var i = 0; i < data.COLUMNS.length; i++) {
     result[j][data.COLUMNS[i].toLowerCase()] = data.DATA[j][i];
  }
}

return result;
};
ujsonURL = URLd.parseCFJSON();
console.log(ujsonURL);
</script>

My console is giving me this error Uncaught ReferenceError: WddxRecordset is not defined

When I try to feed the json coldfusion data to this function like this:

<script>
<cfoutput>
var #toScript(URLd,"URLd")#;
</cfoutput>
Object.prototype.parseCFJSON = function() {

var result = [],
   data = this;

for (var j = 0; j < data.DATA.length; j++) {
  result[j] = {};
  for (var i = 0; i < data.COLUMNS.length; i++) {
     result[j][data.COLUMNS[i].toLowerCase()] = data.DATA[j][i];
  }
}

return result;
};
ujsonURL = jsonURL.parseCFJSON();
console.log(ujsonURL);
</script>

I am getting this error:

Uncaught TypeError: Cannot read property 'length' of undefined

at this line

for (var j = 0; j < data.DATA.length; j++) {

I am doing something wrong and I am really stumped. Any help would be appreciated. I could be using the js function completely wrong (still learning).

4
Sorry I screwed up my comment: In your second snippet: jsonURL is still defined, right? Have you tried outputting your data variable in the console to see exactly what is being passed to the function?Esten
@Esten when I am attempting to pass to it, I am getting these errors.Ren44
what I'm saying, is if you place console.log(this) before the for condition where it errors. It is basically saying that it can't find a value for data.DATA, so I'm curious what the value of this isEsten
@Esten console.log(this) before the 'for' found nothing, maybe I am passing my data to the function incorrectly? The tutorial says pass the struct like this myData = myData.parseCFJSON(); which I thought I was doingRen44
The function is created on the Object's prototype, which means the function would be accessible by all other objects. So when he calls myData = myData.parseCFJSON();, myData needs to be an object. SerializeJson should return a string, not an object, so that is probably why there is an issue when you do jsonURL.parseCFJSON(); What line does the error occur when you do: URLd.parseCFJSON();Esten

4 Answers

1
votes

Have you considered converting the ColdFusion JSON data back to a ColdFusion Query object and then reconverting it back to json using JSONUtil?

JSONUtil has better JSON support for CF7-2016 with "strictMapping" & "serializeQueryByColumns" support.

<cfscript>
JSONString = {}; /* This needs to be the ColdFusion JSON query */
JSONUtil = CreateObject("component","JSONUtil");
QueryData = JSONUtil.DeserializeJSON(JSONString, false);
JSON_oldWDDX = JSONUtil.SerializeJSON(var=QueryData, serializeQueryByColumns=true, strictMapping=true);
JSON_Array = JSONUtil.SerializeJSON(var=QueryData, serializeQueryByColumns=false, strictMapping=true);
</cfscript>
0
votes

Maybe you could try serializing the coldfusion to a string, then passing that into the function. Then, convert the string back to JS JSON with parse()

<cfset jsonURL = SerializeJSON(SmartGuideSearchRet,true)>
<script>
<cfoutput>
var #toScript(jsonURL,"jsonURL")#;
</cfoutput>
function parseCFJSON(json) {

var result = [],
   data = json.parse();

for (var j = 0; j < data.DATA.length; j++) {
  result[j] = {};
  for (var i = 0; i < data.COLUMNS.length; i++) {
     result[j][data.COLUMNS[i].toLowerCase()] = data.DATA[j][i];
  }
}

return result;
};
parsedJSON = parseCFJSON(ujsonURL);
console.log(parsedJSON);
</script>
0
votes

Well I figured out what was going wrong here.

This <cfoutput> var #toScript(jsonURL,"jsonURL")#; </cfoutput> Was not parsing correctly. I changed it to eval() so my updated script looks like this and returns perfectly.

<script>
mydata = eval(<cfoutput>#jsonURL#</cfoutput>)
<cfoutput>
</cfoutput>
Object.prototype.parseCFJSON = function() {

var result = [],
   data = this;

for (var j = 0; j < data.DATA.length; j++) {
  result[j] = {};
  for (var i = 0; i < data.COLUMNS.length; i++) {
     result[j][data.COLUMNS[i].toLowerCase()] = data.DATA[j][i];
  }
}

return result;
};
mydata = mydata.parseCFJSON();
console.log(mydata);
alert(mydata);
</script>
0
votes

Passing "struct" to your serializeJson function also worked for me.

SerializeJSON(var, "struct");