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

228
Vistas
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 Respuestas
Responde la pregunta

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 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