ritz301 ritz301 - 4 months ago 4
Javascript Question

JSON inside JSON-like object javascript

I have generated a json-type variable:

var jsonLines = [[{"x":"275","y":"231"},{"x":"124","y":"237"}],[{"x":"201","y":"157"},{"x":"275","y":"231"}],[{"x":"215","y":"307"},{"x":"201","y":"157"}],[{"x":"342","y":"188"},{"x":"215","y":"307"}]];

I want to parse this JSON-like object and print the corresponding entities. I tried many solutions of similar problems here at SO, but nothing worked on this ( for each loop, by indexing etc.). It'll great if anyone could help me out. Thank you.


JSON is short for JavaScript Object Notation. What you have there is a Javascript object literal , and it can be treated as such.

for(var i = 0; i < jsonLines.length; i++){
    var innerArray = jsonLines[i];

    for(var j = 0; j < innerArray.length; j++){
        var obj = innerArray[j];

        //now you can use obj.x, obj.y etc...

JSON is heavily based off JavaScript object literals so when it is in actual code and not in a string/text file, it is actually a JavaScript object.

You can break-down the object like so

//An Array of...
    //Arrays of
        //JavaScript Object Literals

Also worth nothing that JavaScript Object property names can be strings

var obj1 = {
  a : "someValue"

var obj2 = {
  "a" : "someOtherValue"

//Both of these objects can access property "a"

obj1.a //someValue
obj2.a //someOtherValue