Filtering data from JSON

Hey there, new to Pipedream.

I’m trying to understand the best approach to filtering a JSON response within Pipedream and return it as an HTTP response.

Current scenario:

  1. I’m receiving an HTTP response with a query paramater
  2. I’m pulling data from an API
  3. I need to filter only data with status: active
  4. I need to return a new filtered JSON array.

Here’s my nodejs step:

// To use any npm package, just import it
// import axios from "axios"

export default defineComponent({
  async run({ steps, $ }) {

  const products = steps.get_catalog_from_orderport.$return_value.Data.Products

  let filterSku = products.filter(Sku => steps.get_catalog_from_orderport.$return_value.Data.Products[0].Opsku.match("{steps.trigger.event.query.id}"))
  console.log(filterSku)

    // Reference previous step data using the steps object and return data to use it in future steps
    return filterSku
  },
})

It doesn’t return an error, but it also doesn’t return any data (when there should be 1 record showing).

Appreciate the help!

– –

Edit: I have a few different scenarios – in one instance I need to pull multiple records based on status, another where I need to only find one record based on ID.

Hi @seanpritzkau

First off, welcome to the Pipedream community. Happy to have you!

Thanks for sharing your code step, it’s much easier to follow along and give guidance.

The filter looks good, the .match can be used to detect patterns, and it returns a null if none are found which is a Falsey value.

However, I did notice that you’re only applying the match to the first item in your array of products:

products.filter(Sku => steps.get_catalog_from_orderport.$return_value.Data.Products[0] // rest of expression...

The Sku variable is what you should be referencing. I recommend renaming it to product just so it’s easier to understand that this is a single product in your list of products being filtered:

  let filterSku = products.filter(product => product.Opsku.match("{steps.trigger.event.query.id}"));

Hope this helps!

Thanks @pierce! That did the trick. I also removed the brackets and quotes around my query.id, and that caused the code to execute appropriately.

I’ll leave the code below just for reference for others.

// To use any npm package, just import it
// import axios from "axios"

export default defineComponent({
  async run({ steps, $ }) {

  const products = steps.get_catalog_from_orderport.$return_value.Data.Products

  let filterSku = products.filter(product => product.Opsku.match(steps.trigger.event.query.id))
  console.log(filterSku)

    // Reference previous step data using the steps object and return data to use it in future steps
    return filterSku
  },
})

Really appreciate your help!

1 Like