1

I have a simple JSON string, encapsulated in an array created using JSONArray and JSONObject form org.json in Java.

var outObjA = [{"LoginTime":"2018-02-14 08:51:48.0","User":"f00dl3","RemoteIP":"127.0.0.1"}]; 

I am trying to parse this in JavaScript. First I iterate over the array encapsulating the data using an `i" counter:

for(var i = 0; i < outObjA.length; i++) { var jsonData = JSON.parse(outObjA[i]); console.log(jsonData); } 

When I attempt to parse the JSON, I get an error:

SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data

I built a try/catch into it and it returns an object:

for (var i = 0; i < outObjA.length; i++) { var jsonData = null; try { jsonData = JSON.parse(outObjA[i]); } catch (e) { jsonData = outObjA[i]; } console.log(jsonData); } 

Returned:

{ "LoginTime": "2018-02-14 08:51:48.0", "User": "f00dl3", "RemoteIP": "127.0.0.1" } 

This is valid JSON, is it not?

7
  • Thats not json... that's javascript object, you can't parse a JavaScript object, remove JSON.parse( Commented Feb 14, 2018 at 15:41
  • 2
    outObjA is already an object you don't have to parse it! If it was a string you'd have to. Commented Feb 14, 2018 at 15:42
  • stackoverflow.com/questions/8294088/javascript-object-vs-json Commented Feb 14, 2018 at 15:43
  • it's an object written in JavaScript Object Notation, but since it's already in Javascript it doesn't need to be parsed. Commented Feb 14, 2018 at 15:44
  • @Occam'sRazor JSON cannot be an object, so to say it's an object written .... is confusing. JSON is a string (which I suppose is an object...), that represents an object Commented Feb 14, 2018 at 15:48

3 Answers 3

4

That's not a JSON string, it's a JavaScript array. To make it a JSON string, surround it with apostrophes, then you can parse it, and finally loop through it:

var outObjA = '[{"LoginTime":"2018-02-14 08:51:48.0","User":"f00dl3","RemoteIP":"127.0.0.1"}]'; var outObjA = JSON.parse(outObjA); for (var i = 0; i < outObjA.length; i++) { var jsonData = outObjA[i]; console.log(jsonData); }

Or better, you can loop through it directly without parsing:

var outObjA = [{"LoginTime": "2018-02-14 08:51:48.0", "User": "f00dl3", "RemoteIP": "127.0.0.1"}]; for (var i = 0; i < outObjA.length; i++) { var jsonData = outObjA[i]; console.log(jsonData); }

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

Comments

1

This line is not necessary.

 for(var i = 0; i < outObjA.length; i++) { var jsonData = JSON.parse(outObjA[i]); console.log(jsonData); } 

Because outObjA is a array type not json,it does not need parsing simply retrieve it and display it.

The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned, source.

To expand further take this example from Mozilla ,

var json = '{"result":true, "count":42}'; 

The reason why this needs parsing is because its a string type, the JSON.parse converts that string into a JSON object, thus making it accessible. like this,

obj = JSON.parse(json); console.log(obj.count); // expected output: 42 console.log(obj.result); // expected output: tru 

The only way your code would work is if you did this.

var outObjA = ['{"LoginTime":"2018-02-14 08:51:48.0","User":"f00dl3","RemoteIP":"127.0.0.1"}']; 

This way the element at position zero is a string, not a JSON object.

To conclude, strings are not JSON.

  1. JSON objects are surrounded by curly braces {}.
  2. JSON objects are written in key/value pairs.
  3. Keys must be strings, and values must be a valid JSON data type (string, number, object, array, boolean or null).
  4. Keys and values are separated by a colon.
  5. Each key/value pair is separated by a comma.

1 Comment

So parse strings into JSON format, not other way alright
0

you do not need parse for it is already json

you might use instead

var jsonData = outObjA[i]; console.log(jsonData['User']); // or any other key 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.