0

My question is entirely related to the structure of JSON. I've this:

{
  "cars": {
    "rows": [
      {
        "name": "Mercedes",
        "color": "Black",
        "make": "Mercedes"
      },
      {
        "name": "BMW",
        "color": "Black",
        "make": "BMW Germany"
      },
      {
        "name": "Innova",
        "color": "Red",
        "make": "Toyota"
      }
    ]
  }
}

Till now, row is an array of objects that contains information of different cars. I want two such row arrays. Only two are required. Like this:

{
  "staticKpi": {
    "rows": [
      {
        // 1st row 1st object
      },
      {
        // 1st row 2nd object
      },
      {
        // 1st row 3rd object
      }
    ],
    [
      {
        // 2nd row 1st object
      },
      {
        // 2nd row 2nd object
      },
      {
        // 2nd row 3rd object
      }
    ]
  }
}

You can see this JSON here. But this is giving me JSON error. I just want to keep two lines of objects so there will be only two arrays in rows. hope I was able to explain the problem. Please correct my mistake.

PS: I've run forEach loop later on this JSON. So I've to take care of that too.

2
  • 2
    Why not each items in rows being an array? This structure is invalid : { "rows": [ { } ],[ { } ] }. However, this one is valid { "rows": [ [ { } ], [ { } ] ] } Commented Nov 22, 2020 at 13:01
  • @Cid Will it support multiple objects? Commented Nov 22, 2020 at 13:07

1 Answer 1

1

Try like this:

{
   "staticKpi":{
      "rows":[
         [
            {
               "name":"Mercedes",
               "color":"Black",
               "make":"Mercedes"
            },
            {
               "name":"BMW",
               "color":"Black",
               "make":"BMW Germany"
            },
            {
               "name":"Innova",
               "color":"Red",
               "make":"Toyota"
            }
         ],
         [
            {
               "name":"Mercedes",
               "color":"Black",
               "make":"Mercedes"
            },
            {
               "name":"BMW",
               "color":"Black",
               "make":"BMW Germany"
            },
            {
               "name":"Innova",
               "color":"Red",
               "make":"Toyota"
            }
         ]
      ]
   }
}

rows needs to be one object/array, it can't be two. To have more than one element, you need to put those two elements in an array.

EDIT: To loop through these using forEach in JavaScript, you could do this:

data = {"staticKpi":{"rows":[[{"name":"Mercedes","color":"Black","make":"Mercedes"},{"name":"BMW","color":"Black","make":"BMW Germany"},{"name":"Innova","color":"Red","make":"Toyota"}],[{"name":"Mercedes","color":"Black","make":"Mercedes"},{"name":"BMW","color":"Black","make":"BMW Germany"},{"name":"Innova","color":"Red","make":"Toyota"}]]}};

data.staticKpi.rows.forEach((row) => console.log(row));

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

3 Comments

I just wonder how do i tweak forEach loop for this new structure. Please throw some light on this.
I've added to this answer an example of looping through the rows element (so that you get each of the two).
I just showed this answer to my tech lead. She said this solution is good and we can push it to master. thanks :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.