Abdul Manaf Abdul Manaf - 2 years ago 166
JSON Question

aws lambda return json data with slashes

i need to print a json data using aws lambda. this is my code

'use strict';
console.log('Loading function');

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

var addon = require('./path/to/addon');
var sampleData=addon.getSampleData(userId);
console.log(sampleData); // it will print correct json data
//var sampleData="{ \"data\":{ \"key1\": \"1472722877992\", \"key2\": [ 814, 809] }}";

callback(null, sampleData);

i got output like this

"{ \"data\":{ \"key1\": \"1472722877992\", \"key2\": [ 814, 809] }}"

Bu i need to get output like this

"{ "data":{ "key1": "1472722877992", "key2": [ 814, 809] }}"

in this code, i created a npm library addon using c++ code. and getSampleData is a method inside c++ code. it will return a json formated string(not a json object) .in my node.js code, console log print
correct json string.

But executing this lambda function returnd output with Slashes. How to solve this issue.

Answer Source

"{ "data":{ "key1": "1472722877992", "key2": [ 814, 809] }}" isn't a valid string. It's a double quoted string with non-escaped double quotes embedded in it. That isn't valid.

Have you tried using single quotes for the string, like this?

var sampleData='{ "data":{ "key1": "1472722877992", "key2": [ 814, 809] }}';

Or have you tried just returning a JSON object like this?

var sampleData={ "data":{ "key1": "1472722877992", "key2": [ 814, 809] }};

Edit based on new info in question:

Try converting the string to a JSON object like this:

callback(null, JSON.parse(sampleData));

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download