Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

136
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda