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

133
Vistas
Get payload from String

I have this String:

['TEST1-560', '{"data":[{"price":0.0815,"volume":0.2,"car":"BLUE"}],"isMasterFrame":false}']

I want to get the keys 'TEST1-560' which is always fist and "car" value.

Do you know how I can implement this?

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

0

This is a very, very scuffed code, but it should work for your purpose if you have a string and you want to go through it. This can definitely be shortened and optimized, but assuming you have the same structure it will be fine.:

// Your data
var z = `['TEST1-560', '{"data":[{"price":0.0815,"volume":0.2,"car":"BLUE"}],"isMasterFrame":false}']`;
var testName = z.substring(2).split("'")[0];
var dividedVar = z.split(",");
for (var ind in dividedVar) {
  if (dividedVar[ind].split(":")[0] === '"car"') {
    var car = dividedVar[ind].split(":")[1].split("}")[0].substring(1,dividedVar[ind].split(":")[1].split("}")[0].length-1);
    console.log(car)
  }
}
console.log(testName);

output:

BLUE
TEST1-560

In a real application, you don't need to log the results, you can simply use the variables testName,car. You can also put this in a function if you want to handle many data, e.g.:

function parseData(z) {
    var testName = z.substring(2).split("'")[0];
    var dividedVar = z.split(",");
    for (var ind in dividedVar) {
        if (dividedVar[ind].split(":")[0] === '"car"') {
            var car = dividedVar[ind].split(":")[1].split("}")[0].substring(1, dividedVar[ind].split(":")[1].split("}")[0].length - 1);
        }
    }
    return [testName, car]
}

This will return the variables values in an array you can use

about 4 years ago · Juan Pablo Isaza Denunciar

0

const arr = ['TEST1-560', '{"data":[{"price":0.0815,"volume":0.2,"car":"BLUE"}],"isMasterFrame":false}']

const testValue = arr[0];
const carValue = JSON.parse(arr[1]).data[0].car;

console.log(testValue);

console.log('-----------');
console.log(carValue);

If your structure is always the same, your data can be extracted like above.

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