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?

Racil Hilan
  • 24,690
  • 13
  • 50
  • 55
user3260912
  • 603
  • 2
  • 7
  • 19
  • Thats not json... that's javascript object, you can't parse a JavaScript object, remove `JSON.parse(` – Liam Feb 14 '18 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. – Liora Haydont Feb 14 '18 at 15:42
  • https://stackoverflow.com/questions/8294088/javascript-object-vs-json – messerbill Feb 14 '18 at 15:43
  • it's an object written in **J**ava**S**cript **O**bject **N**otation, but since it's already in Javascript it doesn't need to be parsed. – I wrestled a bear once. Feb 14 '18 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 – Liam Feb 14 '18 at 15:48
  • @Liam - JSON is a *Notation*, a syntax, a set of rules, like HTML or Markdown. To say that it's a string is confusing. It's only a string if it's being represented by some other programming language. And because it's a subset of Javascript, (in other words, it *is* Javascript) it does not need to be parsed (unless it's represented by a string - just as you would have to `eval` javascript code if it was represented by a string.) – I wrestled a bear once. Feb 14 '18 at 16:25
  • How is to say it's a string (`"{'1':'2'}"`) confusing? `"it is a string"`, that's why it's surrounded in `"`. It's less confusing than saying it's an object, because that implies `var x = "{'1':'2'}"; x.1;` would be valid, which isn't...you know, because it's a string. Also Json and Javascript have different syntax `{'1':'2'}` is valid Javascript but invalid JSON, so it's **not** Javascript. – Liam Feb 14 '18 at 16:28

3 Answers3

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);
}
Racil Hilan
  • 24,690
  • 13
  • 50
  • 55
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.
Liam
  • 27,717
  • 28
  • 128
  • 190
Remario
  • 3,813
  • 2
  • 18
  • 25
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
i100
  • 4,529
  • 1
  • 22
  • 20