8

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.

0

5 Answers 5

18

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Jeff. Much Appreciated
1

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.

Comments

0

If using Java, the response can be a user defined object as below

class Handler implements RequestHandler<SQSEvent, CustomObject> {
    public CustomObject handleRequest(SQSEvent event, Context context) {
        return new CustomObject();
    }
}

Sample code can be found here.

Comments

-1

Do this on your result: response.json()

1 Comment

this does not seem to answer the question. Please can you explain in more detail how this is a solution to this question.
-2

You can use:

    res.on('end', function() {
 context.succeed(body.replace(/\\/g, '') );

To replace \ with nothing..

3 Comments

Where do i use this?
I updated the answer, if its not working try json.parse(body)
@JamesG I updated the code again,, replace this code with this res.on('end', function() { context.succeed(body);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.