Copy link to clipboard
Copied
I am trying to generate the following with Coldfusion to pass that as a json object to JS.
[
{
key: "Final List",
values: [
{
"label": "Title I",
"value" : 33
} ,
{
"label": "Title II",
"value" : 33
}
]
}
I have:
postObject["KEY"] = "Final List";
postObject["VALUES"] = [];
postObject.values[1] = {'label':"Title I", 'value':"33"};
postObject.values[2] = {'label':"Title II", 'value':"35"};
serializeJSON(postObject);
The result I get is a bit different:
{"VALUES":[{"value":33,"label":"Title I"},{"value":35,"label":"Title II"},
"key":"Final List"}
Any help appreciated...
Keep result(postObject) in an array. Javascript is case sensitive so its better to use structinsert in that case
resultArray = ArrayNew(1);
structInsert(postObject, "key", "Final List");
structInsert(postObject, "values", "");
TEMPvalues = ArrayNew(1);
TEMPvalues [1] = {'label':"Title I", 'value':"33"};
TEMPvalues [2] = {'label':"Title II", 'value':"35"};
postObject.values = TEMPvalues;
resultArray[1] = postObject;
serializeJSON(resultArray);
Copy link to clipboard
Copied
Keep result(postObject) in an array. Javascript is case sensitive so its better to use structinsert in that case
resultArray = ArrayNew(1);
structInsert(postObject, "key", "Final List");
structInsert(postObject, "values", "");
TEMPvalues = ArrayNew(1);
TEMPvalues [1] = {'label':"Title I", 'value':"33"};
TEMPvalues [2] = {'label':"Title II", 'value':"35"};
postObject.values = TEMPvalues;
resultArray[1] = postObject;
serializeJSON(resultArray);
Copy link to clipboard
Copied
Thanks that was it. Much appreciated!!!
Copy link to clipboard
Copied
@NeoCold,
Alternatively, you could use the original syntax you posted, but put the structure key names in lowercase as you were intending:
postObject["key"] = "Final List";
postObject["values"] = [];
Since you are using the bracket notation for creating structure keys already, ColdFusion will respect the case of the keys you enter in the brackets.
-Carl V.