3

This is my code

var AWS = require('aws-sdk');
AWS.config.region = 'ap-southeast-1';

var uuid = require('node-uuid');
ec2 = new AWS.EC2({apiVersion: 'latest'});


AWS.config.update({accessKeyId: xxxxxxxx, secretAccessKey: yyyyyyyyyyy});
AWS.config.update({region: 'zzzzzzzzzzzzzz'});

var util = require('util');
var opsworks = new AWS.OpsWorks();
var cloudformation = new AWS.CloudFormation();
var cloudwatch = new AWS.CloudWatch();

function listInstances(callback){
new AWS.EC2().describeInstances(function(error, data) {
    if (error) {
        callback(error);
    } else {
        callback(util.inspect(data, {depth: null})); //this returns a full fledged JSON response on the console

}

});
}

//Test above methods
listInstances(callback);


function callback(data){
  console.log(data);
}

I tried parsing the JSON response by replacing the line

callback(util.inspect(data, {depth: null})); 

with

JSON.parse(data);

I get the following error

{ [SyntaxError: Unexpected token o] statusCode: 200, retryable: false }
{ [200: null]
message: null,
code: 200,
time: Tue Jul 22 2014 22:59:42 GMT+0530 (India Standard Time),
statusCode: 200,
retryable: false }

i tried using eval() to parse it and i got the following error

[SyntaxError: Unexpected identifier]

Am I doing anything wrong, should I be parsing the JSON response using any other logic?

Please excuse my ignorance if any since I am new to both node.js and AWS

7
  • Can you console.log(data) so we can see how it looks? Commented Jul 22, 2014 at 17:44
  • this is how it looks { Reservations: [ { ReservationId: 'xx2', OwnerId: 'xxx', Groups: [], Instances: [Object] }, { ReservationId: 'xxx1', OwnerId: 'xxx', Groups: [], Instances: [Object] } ] } Commented Jul 22, 2014 at 17:47
  • 1
    So it's already an object. There's no need to parse anything. Just use it as is. Commented Jul 22, 2014 at 17:49
  • i even tried the following so that the "objects" are further simplified var respdata = util.inspect(data, {depth: null}); JSON.parse(respdata); even then it showed the same error Commented Jul 22, 2014 at 17:51
  • 1
    What exactly are you trying to do? There is no need to call JSON.parse -- you are already holding an object. Just do data.Reservations[0].OwnerId, for example. Commented Jul 22, 2014 at 17:54

1 Answer 1

0

Seems like you do not need to parse data, because it is already parsed.

Look at this answer, it is a similar problem.

I keep getting "Uncaught SyntaxError: Unexpected token o"

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

Comments

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.