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
Parse nested object in JSON string

I have this code:

let test = '{"attribute_as":"plan_id","operator":"fromTo","values":"{"from":"70","to":"80"}"}';
console.log(JSON.parse(test));

It of course fail because in values I have an object. Is there any option how to parse this string in easy way? Or is it not possible at all?

At the end the result should be:

{
    attribute_as: 'plan_id',
    operator: 'fromTo',
    values: {
        from: 70,
        to: 80
    }
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The string is incorrect:

let err = '{"attribute_as":"plan_id","operator":"fromTo","values":"{"from":"70","to":"80"}"}';
// This is the original string
let pass = '{"attribute_as":"plan_id","operator":"fromTo","values":{"from":70,"to":80}}';
// Corrected string

let desiredObj = {
    attribute_as: 'plan_id',
    operator: 'fromTo',
    values: {
        from: 70,
        to: 80
    }
};

console.log(JSON.stringify(desiredObj) == err);
console.log(JSON.stringify(desiredObj) == pass);

about 4 years ago · Juan Pablo Isaza Relatório

0

This should do the trick. When logged both evaluated correctly.

let
    test = '{"attribute_as": "plan_id","operator": "fromTo","values": {"from": 70,"to": 80}}',
    test2 = {
        attribute_as: 'plan_id',
        operator: 'fromTo',
        values: {
            from: 70,
            to: 80
        }
    }

console.log(JSON.parse(test));
console.log(JSON.stringify(test2));
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