-4

I would like to be able to convert the JSON string sent from the server into a JavaScript object on a HMTL page. The raw JSON string data is being displayed, but I would rather display it as a JavaScript object instead.

case '/get_list':
  if (req.method == 'POST') {
    console.log("POST");
    var body = '';
    req.on('data', function(data) {
      body += data;
      console.log("Partial body: " + body);
    });
    req.on('end', async function() {
      console.log("Body: " + body);
      var json = JSON.parse(body)
      console.log("name is " + json.name) // get name

      const {
        Client
      } = require('pg');
      const connectionString = 'postgresql://postgres:password@localhost/app';

      const client = new Client({
        connectionString: connectionString,
      });
      await client.connect(); // create a database connection

      console.log("user input is " + json.name1);
      //Returns the result from the database in a JSON string onto the HTML page    
      const res3 = await client.query('SELECT name, studentno, proname FROM applications WHERE name =$1 LIMIT 1', [json.name1]);
      await client.end();
      // json = res2.rows;
      json = res3.rows;
      var obj = JSON.parse(res3.rows);
      var json_str_new = JSON.stringify(json); //working
      console.log(obj);
      console.log(json_str_new);
      res.end(json_str_new);
    });

  }
  break;
Actual results
{"name":"jake","studentno":10001212,"proname":"asdasdas"}

Expected/required results 
{
  name: 'jake',
  studentno: 10001212,
  proname: 'asdasdas'
}
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • 2
    Parse it on the client? If you're using the Fetch API, call the `json` method of the response? – Andrew Li Apr 14 '19 at 15:19
  • 1
    [`JSON.parse`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse) or [`response.json`](https://developer.mozilla.org/en-US/docs/Web/API/Body/json) if you're using fetch – T.J. Crowder Apr 14 '19 at 15:20
  • 2
    `JSON.parse` converts json into an object, but I don't know what you mean by "I would rather display it as a JavaScript object". Are you just looking for the pretty-print indentation? – Daniel Beck Apr 14 '19 at 15:21
  • 1
    (If so, see here: https://stackoverflow.com/questions/4810841/how-can-i-pretty-print-json-using-javascript ) – Daniel Beck Apr 14 '19 at 15:21
  • 1
    From the OP's comment [here](https://stackoverflow.com/questions/55676856/how-to-convert-json-string-sent-from-a-server-into-a-javascript-object#comment98038990_55676953), this is a duplicate of https://stackoverflow.com/questions/4810841/how-can-i-pretty-print-json-using-javascript. – T.J. Crowder Apr 14 '19 at 15:39

1 Answers1

0

If you're planning on using the JSON for anything, as in traversing and reading the data from that, then JSON.parse() is exactly what you need. Pretty-printing is something only useful for humans, so unless you're exclusively using the output for human consumption, the result you have should be fine.

However, if you are going to just show the data, then I would recommend just formatting the output into some HTML/CSS display.

But assuming you are planning on using the data for something, as mentioned before and by others, JSON.parse() is all you need to generate a JS object.

Rocketman173
  • 101
  • 1
  • 1