2

I'm messing around with building a JSON object, so I can push results from a DB query into it, Then returning the populated JSON data.. But running into a problem from the get go.

This is My current code:

//var AppenateResString = {name:"777"};

var AppenateResString = {
  "DataSources": [{
    "Id": "TEST",
    "Rows": [
      ["10011", "10011 - Test Generic Project"]
    ],
    "NewRows": [],
    "DeletedRows": []
  }],
  "LastUpdated": ""
};

var AppenateResBody = JSON.parse(AppenateResString);
response.body = JSON.stringify(AppenateResBody);

But this is the Error I'm getting:

JavaScript Error: SyntaxError: invalid character 'o' looking for beginning of value
    at 
<anonymous>:21:23
--
};

var AppenateResBody = JSON.parse(AppenateResString);
                      ^ Error!

I've narrowed it down to something is wrong with my JSON layout.. or how I'm building the JSON object.. or some basic just, "This is how you do JSON" kindof thing. But I can't figure it out.

Any ideas?

P.S. As you can see I have commented out just what I thought would be a very simple JSON object to build.. it fails the same.

DarbyM
  • 1,173
  • 2
  • 9
  • 25
  • that's not json. that's just a regular javascript variable assignemt. there's no need to parse anything,b ecause that "json" was parsed when the entire ` – Marc B Feb 11 '16 at 21:54
  • 2
    Since that is just a variable assignment of actual javascript object/array data, you're probably getting a magic "stringified" version of that, which would be the text `[object Object]`, which is an invalid array definition in JSON terms, hence the `unexpected o` – Marc B Feb 11 '16 at 21:56
  • Possible duplicate of [I keep getting "Uncaught SyntaxError: Unexpected token o"](http://stackoverflow.com/questions/8081701/i-keep-getting-uncaught-syntaxerror-unexpected-token-o) – Brian Ray Feb 11 '16 at 22:04

5 Answers5

6

AppenateResString isn't JSON at all. It is a JavaScript object (and not a string).

JSON is a serialisation format. If you have any in JavaScript then it will be expressed as a string.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
2

This is a javascript object

var AppenateResString = {
  "DataSources": [{
    "Id": "TEST",
    "Rows": [
      ["10011", "10011 - Test Generic Project"]
    ],
    "NewRows": [],
    "DeletedRows": []
  }],
  "LastUpdated": ""
};

if you want it as a JSON string then

var str = JSON.stringify(AppenateResString);

if you want the JSON string back to an javascript object then

var obj = JSON.parse(str);
BenG
  • 14,826
  • 5
  • 45
  • 60
  • Thank You. Many good answers here, but this one is worded in a way that clicks with me. Finally makes a little more sense now, Got rid of the parse all together, and now can use it as an object just like I want. Will do the JSON.stringify() just to put it back into "JSON data" to send as my response. – DarbyM Feb 11 '16 at 22:09
1

Your just over complicating things. You don't need to JSON.parse the object. It's already in the format you need.

    <script type="text/javascript">
var AppenateResString = {
  "DataSources": [{
    "Id": "TEST",
    "Rows": [
      ["10011", "10011 - Test Generic Project"]
    ],
    "NewRows": [],
    "DeletedRows": []
  }],
  "LastUpdated": ""
};

response.body = JSON.stringify(AppenateResString);
    </script>
jjwdesign
  • 3,272
  • 8
  • 41
  • 66
0
var AppenateResBody = AppenateResString;

Instead of

var AppenateResBody = JSON.parse(AppenateResString);

It is an object

See Below:

I keep getting "Uncaught SyntaxError: Unexpected token o"

Community
  • 1
  • 1
D.Jacobi
  • 29
  • 5
0

You're attempting to call JSON.parse on something that's already an object, and that's why you're getting the error.

The error message stating invalid character 'o' looking for beginning of value is happening because it's literally attempting to parse the word 'object' instead of an actual data structure.

jmcgriz
  • 2,819
  • 1
  • 9
  • 11