Abdul Manaf
Abdul Manaf

Reputation: 5003

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.

Upvotes: 4

Views: 5826

Answers (1)

Mark B
Mark B

Reputation: 200850

"{ "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));

Upvotes: 4

Related Questions