Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON returning with "\" (Lambda)

I am using AWS Lambda to get JSON from the open weather api and return it.

Here is my code:

var http = require('http');

exports.handler = function(event, context) {
    var url = "http://api.openweathermap.org/data/2.5/weather?id=2172797&appid=b1b15e88fa797225412429c1c50c122a";
    http.get(url, function(res) {
        // Continuously update stream with data
        var body = '';
        res.on('data', function(d) {
            body += d;
        });
        res.on('end', function() {
            context.succeed(body);
        });
        res.on('error', function(e) {
            context.fail("Got error: " + e.message);
        });
    });
}

It works and returns the JSON, but it is adding backslashes before every " like so:

"{\"coord\":{\"lon\":145.77,\"lat\":-16.92},\"weather\":[{\"id\":803,\"main\":\"Clouds\",\"description\":\"broken clouds\",\"icon\":\"04d\"}],\"base\":\"cmc stations\",\"main\":{\"temp\":303.15,\"pressure\":1008,\"humidity\":74,\"temp_min\":303.15,\"temp_max\":303.15},\"wind\":{\"speed\":3.1,\"deg\":320},\"clouds\":{\"all\":75},\"dt\":1458518400,\"sys\":{\"type\":1,\"id\":8166,\"message\":0.0025,\"country\":\"AU\",\"sunrise\":1458505258,\"sunset\":1458548812},\"id\":2172797,\"name\":\"Cairns\",\"cod\":200}"

This is stopping my over service using (SwiftJSON) detecting this as valid JSON.

Can anyone tell me how to make the API information come out as correctly formatted JSON?

I tried .replace like so:

 res.on('end', function() {

        result = body.replace('\\', '');
        context.succeed(result);
    });

It did not change anything. Still had the same output.

like image 720
JamesG Avatar asked Mar 21 '16 01:03

JamesG


People also ask

How do I return a response in Lambda?

Returning a valueIf you use the RequestResponse invocation type, such as Synchronous invocation, AWS Lambda returns the result of the Python function call to the client invoking the Lambda function (in the HTTP response to the invocation request, serialized into JSON).

What should a Lambda handler return?

When you call it, Lambda waits for the event loop to be empty and then returns the response or error to the invoker. The response object must be compatible with JSON. stringify . For asynchronous function handlers, you return a response, error, or promise to the runtime instead of using callback .

Can Lambda continue after returning response?

Lambda manages the function's asynchronous event queue and attempts to retry on errors. If the function returns an error, Lambda attempts to run it two more times, with a one-minute wait between the first two attempts, and two minutes between the second and third attempts.

What does Lambda function return?

Lambda functions are syntactically restricted to return a single expression. You can use them as an anonymous function inside other functions. The lambda functions do not need a return statement, they always return a single expression.


2 Answers

You're posting it as a string.

Try context.succeed(JSON.parse(result))

From the docs

The result provided must be JSON.stringify compatible. If AWS Lambda fails to stringify or encounters another error, an unhandled exception is thrown, with the X-Amz-Function-Error response header set to Unhandled.

http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html

So essentially it's taking your json string as a string and calling JSON.stringify on it...thus escaping all the quotes as you're seeing. Pass the parsed JSON object to succeed and it should not have this issue

like image 193
Jeff Avatar answered Sep 22 '22 07:09

Jeff


In case of Java, just return a JSONObject. Looks like when returning string it is trying to do some transformation and ends up escaping all the quotes.

like image 44
JoeV Avatar answered Sep 18 '22 07:09

JoeV