user829174
user829174

Reputation: 6362

Getting SyntaxError: Unexpected token u in JSON at position 0 when running Lambda Test

running Lambda test with

{
  "var1": "2017-04-17T18:48:03.608Z",
  "var2": "0.45",
  "var3": "0.5"
}

Function:

exports.handler =  (event, context, callback) => {

    console.log("event.body = " + event.body);
    const {var1, var2, var3} = JSON.parse(event.body);
    const tmpItem = {
        "var_1": var1,
        "var_2": var2,
        "var_3": var3
    };
    console.log('Inserting item');
}

gives the following exception:

Request ID: "3aa87175-d544-11e8-ab0a-2b268a563fb1"

Function Logs:

START RequestId: 3aa87175-d544-11e8-ab0a-2b268a563fb1 Version: $LATEST
2018-10-21T15:16:05.617Z    3aa87175-d544-11e8-ab0a-2b268a563fb1    event.body = undefined
2018-10-21T15:16:05.636Z    3aa87175-d544-11e8-ab0a-2b268a563fb1    SyntaxError: Unexpected token u in JSON at position 0
    at JSON.parse (<anonymous>)
    at exports.handler (/var/task/index.js:18:89)
END RequestId: 3aa87175-d544-11e8-ab0a-2b268a563fb1
REPORT RequestId: 3aa87175-d544-11e8-ab0a-2b268a563fb1  Duration: 82.98 ms  Billed Duration: 100 ms     Memory Size: 128 MB Max Memory Used: 25 MB  
RequestId: 3aa87175-d544-11e8-ab0a-2b268a563fb1 Process exited before completing request

What seems to be the issue?

Upvotes: 13

Views: 14685

Answers (4)

MikeF
MikeF

Reputation: 1018

The test is sent to Lamda as a JSON object, not as a string.

you should be able to access the elements as:

exports.handler =  (event, context, callback) => {
    console.log("event.body = " + event);
    //const {var1, var2, var3} = JSON.parse(event.body);
    const tmpItem = {
        "var_1": event.var1,
        "var_2": event.var2,
        "var_3": event.var3
    };
console.log('Inserting item');

}

Upvotes: 0

Menelaos Kotsollaris
Menelaos Kotsollaris

Reputation: 5506

If you have a look at your log message, you will see that event.body = undefined. If you try JSON.parse(undefined) in your console, you will be able to see Uncaught SyntaxError: Unexpected token u in JSON error.

In my case, I needed to configure by Gateway API in order to mock my response, thus this is what I did in my lambda function:

exports.handler = function(event, context, callback) {
    var testData = getTestData();
    callback(null, { "statusCode" : 200, "body" : JSON.stringify((testData))});
};

function getTestData() {
    var test= [ 
        {
            name: "Name1"
        },
        {
            name: "Name2"
        },
        {
            name: "Name3"
        }
        ];
    return test;
}

The above creates an endpoint which spits out the predefined mock response.

Upvotes: 0

Ayush Gupta
Ayush Gupta

Reputation: 9285

This is because when you test your lambda with a packet, say

{
  "var1": "2017-04-17T18:48:03.608Z",
  "var2": "0.45",
  "var3": "0.5"
}

Then that packet is passed as the event to the handler.

However, you are doing JSON.parse(event.body), but for the above packet, event.body is undefined(which has token u at position 0).

You should change your test packet to:

{
    "body": "{\"var1\":\"2017-04-17T18:48:03.608Z\",\"var2\":\"0.45\",\"var3\":\"0.5\"}"
}

Note that the body is a stringified JSON, because API Gateway Lambdas expect event body in a stringified format.

Upvotes: 40

Rashad Ibrahimov
Rashad Ibrahimov

Reputation: 3309

The reason maybe you don't use callback in your function. Try to add the following

var response = {
    statusCode: 200,
    headers: {
        "x-custom-header" : "my custom header value"
    },
    body: JSON.stringify(tmpItem)
};

// null is for error - means no error
// response object should be JSON.stringify compatible and has format described above.
callback(null, response);

Upvotes: -1

Related Questions