Extracting the initial element from JSON array data in Zapier workflows

I’m not really a developer but I need some help with JSON data manipulation in Zapier.

I’m working with a gaming stats API that sends back player match data. The problem is that the API response includes information for multiple matches (around 10 records) but I only need the most recent one for my automation.

Here’s my current code setup:

axios.get('https://api.gameserver.com/v2/player/87654321/matches/recent?token=<secret_key>')
  .then(function(response) {
   return response.data;
  })
  .then(function(data) {
    output(null, data);
  })
  .catch(output);

The API returns something like this:

{
  "playerId": 87654321,
  "matches": [
    {
      "matchId": 9876543210,
      "valid": true,
      "mode": "RANKED",
      "type": "COMPETITIVE",
      "category": "SOLO_QUEUE",
      "arena": 1,
      "side": "blue",
      "character": 45,
      "ability1": 7,
      "ability2": 12,
      "playerLevel": 30,
      "pointsGained": 89,
      "timestamp": 1675234567890,
      "performance": {
        "finalLevel": 16,
        "goldCollected": 12450,
        "deaths": 3,
        "creepsKilled": 156,
        "eliminations": 7,
        "goldUsed": 11200,
        "totalDamage": 34567,
        "damageTaken": 28901,
        "team": "blue",
        "victory": true,
        "equipment1": 2001,
        "equipment2": 2045,
        "equipment3": 3078,
        "assists": 12
      }
    },
    {
      "matchId": 9876543209,
      "valid": true,
      "mode": "RANKED"
    }
  ]
}

In Zapier the data shows up as nested arrays and I can see all the match records. I just want to grab the first match entry and its stats. How can I modify my code or add another step to extract only the initial array element? Any simple solution would be great.

Just modify your output line to grab the first match instead of the whole data object. Change your last .then() block to:

.then(function(data) {
  const firstMatch = data.matches[0];
  output(null, firstMatch);
})

This sends only the most recent match to your next Zapier step. I did the same thing with a sports API that returned multiple games - worked great and made everything cleaner since you’re dealing with one object instead of arrays later on.

You could also handle this with a separate Code step if you want to keep the API call as-is. Just create a new Code by Zapier action after your current one and use inputData.matches[0] to grab the first element. I’ve found this approach really useful when APIs sometimes return different response structures - gives you more flexibility to add validation or handle edge cases like empty arrays. Plus the separate step makes debugging way easier since you can see exactly what data’s flowing between each action in your Zap history.

you can do this directly in zapier’s interface - no code needed. after your webhook/api step, add a ‘formatter’ action and choose ‘utilities > pick from list’. set it to grab item 1 from the matches array and zapier handles everything else. much easier than javascript if you’re not comfortable coding.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.