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

Maheedhar
  • 65
  • 1
  • 10
  • Can you `console.log(data)` so we can see how it looks? – Nitzan Shaked Jul 22 '14 at 17:44
  • this is how it looks { Reservations: [ { ReservationId: 'xx2', OwnerId: 'xxx', Groups: [], Instances: [Object] }, { ReservationId: 'xxx1', OwnerId: 'xxx', Groups: [], Instances: [Object] } ] } – Maheedhar Jul 22 '14 at 17:47
  • 1
    So it's already an object. There's no need to parse anything. Just use it as is. – Nitzan Shaked Jul 22 '14 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 – Maheedhar Jul 22 '14 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. – Nitzan Shaked Jul 22 '14 at 17:54
  • @nitzan : that worked.. Seems like a silly question that i asked.. But i hope u can excuse it since i am very new to JavaScript.. Thanks a lot for your time.. – Maheedhar Jul 22 '14 at 17:58
  • no problems, you're welcome. – Nitzan Shaked Jul 22 '14 at 17:59

1 Answers1

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"

Community
  • 1
  • 1
Daniel777
  • 851
  • 8
  • 20