Skip to content
Advertisement

Extracting a particular Project ID from Asana Task API via Node.js JSON output

Using the Asana Task API, we’re able to see the list of projects that a task belongs to, as well as those projects’ GID and Notes (description text).

Desired outcome

The entire goal here is to grab the GID of the project that has #websiteprojecttemplate within its Notes value. We need to find the GID of that project, and then output it so we can use that GID later on in our Zapier actions.

Asana Task API

Using this API URL, we can see the output of the API which returns the data of the projects (including the GID and Notes). I believe this is JSON. https://app.asana.com/api/1.0/tasks/{id}?opt_fields=projects.notes

For example: https://app.asana.com/api/1.0/tasks/1799885428032109?opt_fields=projects.notes shows as: 

https://i.imgur.com/zSizMNC.png


Current code

Ideally, the Node.js Javascript code would be able to iterate/search/find the one with #websiteprojecttemplate and then output the matching GID. In this case, it’d be: 1199916857565229

This is the JS code I have so far:

const res = await fetch('https://app.asana.com/api/1.0/tasks/' + inputData.uniqueID + '?opt_fields=projects.notes', {
    headers: {
        'Authorization': 'Bearer 0/899removedforsecurity24564s'
    }
});
const body = await res.json();
const projects = body.data;

output = { id: projects };

Which outputs something like:

  "id" : {
    "gid" : "1199885428032109",
    "projects" : [ {
      "gid" : "810573962916457",
      "notes" : "CRM project to create custom fields for the CRM task"
    }, {
      "gid" : "881219806802782",
      "notes" : "Helps keep PMs aware of what stage of progress the website projects are at; as well as how many projects each PM has (via saved Asana searches)."
    }, {
      "gid" : "1129624391492919",
      "notes" : "Tracks the stage and progress of converting a lead to a client."
    }, {
      "gid" : "1140671985497468",
      "notes" : "Additional CRM project to create more custom fields for the CRM task"
    }, {
      "gid" : "1199916857565229",
      "notes" : "Created from the #websiteprojecttemplate."
    } ]
  }

But I need it to output not the entire body’s data, but only the gid value that had #websiteprojecttemplate in its notes value.


TL;DR

How do we update the current code so that it can iterate through the gid and find the one that has #websiteprojecttemplate in the notes, and output the gid number of it?

Advertisement

Answer

I think your question boils down to how to find an element in an array.

const project = projects.projects.find(p => p.notes.includes("#websiteprojecttemplate"));
if (project) {
  console.log(project.gid);
} else {
  console.log("no matching project");
}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement