1

I am creating a POST method api end point. I am sending the payload as a JSON string in RAW format from the postman. Then I am doing JSON.parse in my deno app controller. But the output after doing the JSON.parse is still a JSON string.

This is my controller code:

import { IResponse } from '../models/response.ts'
import { IFulllName } from '../models/full-name.ts'

export const printFullName = async (
  { request, response }: { request: any, response: any }) => {

    const body = await request.body();
    let value = body.value;
    console.log(value, '||| value');
    console.log(typeof(value), '||| value type');

    value = JSON.parse(JSON.stringify(value));

    // This should be a JSON object but it still logs as a string

    console.log(value, '||| value');
    console.log(typeof(value), '||| value type');
}

If I do JSON.parse directly without JSON.stringify, I get a 500 internal server error (no error in the logs as well).

Here is the payload that I have tried:

'{"firstName": "First", "lastName": "Last"}'

Note: I am using oak along with Deno.

What I need is to convert the JSON string into a JSON object in the controller code.

In case there is any inbuilt way of doing so using any oak method, please do suggest.

atish
  • 495
  • 5
  • 18
  • Show the output of the first `console.log(value, '||| value');` – Marcos Casagrande Jun 07 '20 at 12:53
  • @Marcos: This is the output of first console: '{"firstName": "First", "lastName": "Last"}' ||| value string ||| value type For second console after JSON.parse nothing modifies: '{"firstName": "First", "lastName": "Last"}' ||| value string ||| value type – atish Jun 07 '20 at 12:59

1 Answers1

2

You're sending an invalid JSON object, in Postman you should POST:

{"firstName": "First", "lastName": "Last"}

without being wrapped by '.


const postedJSON = `'{"firstName": "First", "lastName": "Last"}'`;

// Invalid JSON for JSON.parse, remove '

const stringified = JSON.stringify(postedJSON);

console.log(stringified); // a valid JSON string

console.log(JSON.parse(stringified) === postedJSON);

In any case, the correct way to read a JSON payload in Oak is:

app.use(async ({ request, response }) => {
  const result = await request.body();
  console.log(result.value)
});

Be sure to send Content-Type: application/json

Marcos Casagrande
  • 37,983
  • 8
  • 84
  • 98
  • I tried that as well along many other hit and trials. The output after JSON.parse and before is the same. The value type is still string. – atish Jun 07 '20 at 13:04
  • Don't use `JSON.stringify` just `JSON.parse` and post the error. Wrap a JSON.parse in `try/catch` and show the string passed to it and the error. – Marcos Casagrande Jun 07 '20 at 13:07
  • I tried with this payload: {"firstName": "First", "lastName": "Last"} with only JSON.parse (didn't use JSON.stringify). It didn't throw any error but the value after JSON.parse is still a string. Sure, I will check the oak update. – atish Jun 07 '20 at 13:15
  • 1
    This worked @marcos. Thanks! I was missing : Content-Type: application/json – atish Jun 08 '20 at 05:42