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

149
Visualizações
updating an existing json with additional json object

I'm writing a code where I'm building a JSON from another JSON using some conditions. Here is my code.

var data = {
  "results": [{
    "rawData": {
      "description": "test desc",
      "name": "Plans",
      "c_activeInAnswers": true,
      "c_photo": {
        "url": "https://example.com/images/logo.png"
      }
    }
  }]
};

var answerJson = data.results[0].rawData;
var answerText = answerJson.description ?
  answerJson.description :
  answerJson.answer;


var richResult = {
  richContent: [
    [{
      type: "info",
      title: answerText,
    }, ],
  ],
};

if (answerJson.c_photo) {
  var img = {
    src: {
      rawUrl: answerJson.c_photo.url,
    },
  };
/* answerJson.push(img) */;
  Object.keys(richResult.richContent).map(
    function(object) {
      richResult.richContent[object].push(img);
    });
}

console.log(JSON.stringify(richResult));

when I run this. src is displayed as a separate object., but I want it to be inside the existing object. Here is what I get as my current output.

{
  "richContent": [
    [
      {
        "type": "info",
        "title": "test desc"
      },
      {
        "src": {
          "rawUrl": "https://example.com/images/logo.png"
        }
      }
    ]
  ]
} 

How can I change it to look like

{"richContent": [
    [
      {
        "type": "info",
        "title": "test desc",
        "image": {
          "src": {
            "rawUrl": "https://example.com/images/logo.png"
          }
        }
      }
    ]
  ]
} 
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Instead of using the Object.keys code you could try the following approach.

richResult.richContent[0][0].img = img;

Complete code is as follows

var data = {
    "results": [{
        "rawData": {
            "description": "test desc",
            "name": "Plans",
            "c_activeInAnswers": true,
            "c_photo": {
                "url": "https://example.com/images/logo.png"
            }
        }
    }]
};

var answerJson = data.results[0].rawData;
var answerText = answerJson.description ?
    answerJson.description :
    answerJson.answer;


var richResult = {
    richContent: [
        [
            {
                type: "info",
                title: answerText,
            }
        ]
    ]
};

if (answerJson.c_photo) {
    var img = {
        src: {
            rawUrl: answerJson.c_photo.url
        }
    };

    richResult.richContent[0][0].img = img;   
}

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