coldfusion json parsing issue

I have the following json string and need to extract each of the survey_id values as a list. ie 74448500, 74052991, 65442357

{
   "status":0,
   "data":{
      "surveys":[
         {
            "survey_id":"74448500"
         },
         {
            "survey_id":"74052991"
         },
         {
            "survey_id":"65442357"
         }
      ],
      "page":1,
      "page_size":1000,
      "metadata":{
         "collaboration":{
            "shared_by_total":0,
            "unfiled_owned_total":143,
            "shared_with_total":0,
            "owned_total":242
         }
      }
   }
}

Upvotes: 1

Views: 78

Answers (1)

John Whish
John Whish

Reputation: 3036

Not sure what version of ColdFusion you are on, here are two possible ways to do it:

<cfscript>
    x = deserializeJSON('{"status":0,"data":{"surveys":[{"survey_id":"74448500"},{"survey_id":"74052991"},{"survey_id":"65442357"}],"page":1,"page_size":1000,"metadata":{"collaboration":{"shared_by_total":0,"unfiled_owned_total":143,"shared_with_total":0,"owned_total":242}}}}');

    // ColdFusion 11
    y = x.data.surveys.map(function(item){
        return item.survey_id;
    });


    writeDump(arrayToList(y));

    // ColdFusion 9+
    z = [];
    for (item in x.data.surveys) {
        arrayAppend(z, item.survey_id);
    }

    writeDump(arrayToList(z));

</cfscript>

Upvotes: 4

Related Questions