3 Replies Latest reply on Mar 8, 2013 8:22 AM by Carl Von Stetten

    Generating Json


      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"};



      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...

        • 1. Re: Generating Json
          Ashish Todon Level 1

          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;



          • 2. Re: Generating Json
            NeoCold Level 1

            Thanks that was it.  Much appreciated!!!

            • 3. Re: Generating Json
              Carl Von Stetten Adobe Community Professional & MVP



              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.