Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

177
Vistas
How do you pull specific data from an external API using axios

So I have an external API that I would like to filter out. Part of the API shows this information (in JSON format as an example:

{
    "data": [
        {
            "first_name": "Fiona",
            "last_name": "Smith",
            "phone_number": "987-3595-89",
            "mental_health_referral": false,
            "date_last_mental_health_referal": "02/09/2018T00:00:00.0000Z",
            "legal_councel_referal": true,
            "CHW_id": 6866318
        },
        {
            "first_name": "Richard",
            "last_name": "Stewart",
            "phone_number": "281-0394-41",
            "mental_health_referral": true,
            "date_last_mental_health_referal": "03/23/2018T00:00:00.0000Z",
            "legal_councel_referal": false,
            "CHW_id": 9241074
        },
}

Now I am using axios to call all this data. This is the code:

//endpoint that will fetch data from an external API
app.get("/externalapi/", (req, res, next) => {
    let apiURL = 'https://test.herokuapp.com/api/v1/data';
   
      axios.get(apiURL)
          .then(response => {
              
              res.status(200).json(response.data);
          })
          .catch((err) => {
              res.status(500).json({ message: err });
          });
  });

However, I would like to filter it out to where a specific endpoint would show me only Fiona's information and only show me Fiona's first name, last name, and phone number. I tried filtering it out using this endpoint

app.get("/externalapi/:first_name", (req, res, next) => {

However, it's only giving me errors when I test it out on Postman. What am I missing?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Can write a general function that will take the array and the name of the person you want to return:

const data = {
    data: [
        {
            "first_name": "Fiona",
            "last_name": "Smith",
            "phone_number": "987-3595-89",
            "mental_health_referral": false,
            "date_last_mental_health_referal": "02/09/2018T00:00:00.0000Z",
            "legal_councel_referal": true,
            "CHW_id": 6866318
        },
        {
            "first_name": "Richard",
            "last_name": "Stewart",
            "phone_number": "281-0394-41",
            "mental_health_referral": true,
            "date_last_mental_health_referal": "03/23/2018T00:00:00.0000Z",
            "legal_councel_referal": false,
            "CHW_id": 9241074
        },
        ]
}

const getData = (arr, name) => {
  const nameData = arr.filter(el => (el.first_name === name))
  const returnedObj = nameData[0]
  return returnedObj
}

const filteredData = getData(data.data, "Fiona")

const {first_name, last_name, phone_number} = filteredData
console.log(first_name)
console.log(last_name)
console.log(phone_number)

about 4 years ago · Juan Pablo Isaza Denunciar

0

you can try filtering out the response data using the first name

const userData = response.data.filter((selection)=>{ 
return selection.first_name === 'Fiona';

});

console.log(userData);

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda