Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

338
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!