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

232
Visualizações
access object paratamters node.js

I am logging a response that I get from the server after sending a post request, and it looks like this:

{"output":"basic question #1. how do I calculate the area of a rectangle based on users input in python?"}

now i want to access output as type plain text (string). I tried this:

 console.log(body)
     //this returns  {"output":"basic question #1. how do I calculate the area of a rectangle based on users input in python?"}
 console.log(JSON.stringify(body))
    //this returns "{\"output\":\"calculate the area of a rectangle based on users input in python\"}"

how would I access output within the body response?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There is a built-in method called JSON.parse() in JavaScript that will convert JSON to a JavaScript object.


Follow these steps to get the result you want!

  1. Skip if already parsed! First, you need to use JSON.parse() to parse the string JSON into a valid JavaScript object. As a parameter, you pass in the string that you want to parse.

    JSON.parse(body);
    
  2. To access the body, you can either use dot notation or bracket notation. Either one will work; you use them like below.

    // Dot notation
    body.output;
    
    // Bracket notation
    body["output"];
    

The final code should look like this.

const body = `{"output": "Calculate the area of a rectangle based on users input in Python."}`;

const parsed = JSON.parse(body);

// Dot notation
console.log(parsed.output);

// Bracket notation
console.log(parsed["output"]);


In conclusion, the JSON.parse() method built-into JavaScript should help solve the issue.

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