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

336
Vistas
How to extract the last element out of an invalid json string with js

I'm using a nodejs speech recognition API that returns a string with multiple texts inside a JSON like structure which is invalid and unusable

{
  "text": "Play"
}
{
  "text": "Play astronaut"
}
{
  "text": "Play astronaut in the"
}
{
  "text": "Play astronaut in the ocean"
}
{
  "entities": {},
  "intents": [],
  "text": "Play astronaut in the ocean.",
  "traits": {}
} 

and I only want last element which is

{
  "entities": {},
  "intents": [],
  "text": "Play astronaut in the ocean.",
  "traits": {}
}

by filtering it out or something to extract the text "Play astronaut in the ocean."

The API always outputs multiple text elements in different quantities each time, but I want the last element is that possible? or should I find a different API that's usable?

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

0

Assuming the response is returned as an array, ie [{ "text": "Play" }, { next one.. }] etc

Then you can get the last element in the array by variablising the array, get its length, and find the last object.

e.g.

const x = [{ text.. }, { text.. }]
const lastItem = x[x.length - 1]

Then if you also want to find the last item within that object, you could also do that with Object.values

const lastItemObject = Object.values(lastItem)
const lastItemInTheObject = lastItemObject[lastItemObject.length - 1]
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