Search code examples
google-apps-scripttrello

Json undefined issue


I am having an issue with my json parsing where I keep getting undefined when i try to loop through my json data.

Here is the sample Json code that keeps returning undefined:

 [{"id":"5f99b552b2c25b37596871e3","value":{"number":"1"},"idCustomField":"5f998faf4bb46e574ac28514","idModel":"5f907f344b88092b1d0e03d1","modelType":"card"}]

I am using the following to get the payload and then parse it:

        var response2 = UrlFetchApp.fetch(url + "cards/" + card.id + "/customFieldItems?"+ key_and_token);  
        customfielditems = JSON.parse(response2.getContentText()).actions;

When I try to loop through my object I get an undefined value for my object. Loop listed below:

for (var m=0; m < customfielditems.length; m++) {

customfielditemid = customfielditems[m].value

}

Mind you I don't even get to the point where I provide a value. I get an error indicating ".length" is undefined. Focusing the object, the object itself (customfielditems) is also undefined even after its parsed.

Would love links or suggestions on what I am doing wrong.


Solution

  • From the URL of your script, I thought that you might be using "Get Custom Field Items for a Card". When the official document of "Get Custom Field Items for a Card" is seen, it seems that the response value is Array<CustomFieldItems>. And the sample response value is as follows.

    [
      {
        "id": "5abbe4b7ddc1b351ef961414",
        "value": {
          "checked": "true"
        },
        "idCustomField": "5abbe4b7ddc1b351ef961414",
        "idModel": "5abbe4b7ddc1b351ef961414",
        "modelType": "card"
      }
    ]
    

    From above response value, I understood that your response2.getContentText() is the value of [{"id":"5f99b552b2c25b37596871e3","value":{"number":"1"},"idCustomField":"5f998faf4bb46e574ac28514","idModel":"5f907f344b88092b1d0e03d1","modelType":"card"}] in your question. If my understanding is correct, in this case, customfielditems of customfielditems = JSON.parse(response2.getContentText()).actions; is undefined. I thoutht that the reason of your issue is due to this.

    When you want to retrieve value from the response value, how about the following modification?

    Modified script:

    var response2 = UrlFetchApp.fetch(url + "cards/" + card.id + "/customFieldItems?"+ key_and_token);
    customfielditems = JSON.parse(response2.getContentText());
    
    for (var m=0; m < customfielditems.length; m++) {
      customfielditemid = customfielditems[m].value;
    }
    
    • When you want to retrieve the value of 1 of {"number":"1"}, please modify customfielditemid = customfielditems[m].value; as follows.

        customfielditemid = Object.values(customfielditems[m].value); // or Object.values(customfielditems[m].value)[0];
      

    Note:

    • In this modified script, it supposes that you have already been able to use the API using url + "cards/" + card.id + "/customFieldItems?"+ key_and_token. Please be careful this.

    Reference: