user3125559
user3125559

Reputation: 85

Concat JSON object into array

How can I concat JSON objects if the first object is:

{
     "total": "2"
}

And second:

[
     "player1": {
          "score": "100",
          "ping": "50"
     },
     "player2": {
          "score": "100",
          "ping": "50"
     }
]

If I want final result to look like:

{
     "total": "2",
     {
          "player1": {
               "score": "100",
               "ping": "50"
          },
          "player2": {
               "score": "100",
               "ping": "50"
          }
     }
}

I am trying to do it in JavaScript.

Update

The final result is not a valid array. What about this?

{
     [
          "player1": {
               "score": "100",
               "ping": "50"
          },
          "player2": {
               "score": "100",
               "ping": "50"
          }
     ]
}

Upvotes: 2

Views: 844

Answers (3)

Roko C. Buljan
Roko C. Buljan

Reputation: 206636

As I've said in the comments,
you're trying to append an invalid Array getting as a result an invalid JSON tree.

To validate an expected JSON you can test it here: http://jsonlint.com/

What's wrong 1:

[ "player1" : { "score": "100", "ping": "50"} ] // Array ??
            ^ invalid delimiter (should be ,)

What's wrong 2 in the expected result:

 {
     "property" : "value" ,
     "total" : "2" ,
          { "player1" : { "score": "100", "ping": "50" }
     ^^^^^ where's the property here???  
 }

What you can do:

var myObject = {"total":"2"};    
var players = {
         "player1": {
              "score": "100",
              "ping": "50"
         },
         "player2": {
              "score": "100",
              "ping": "50"
         }
};

myObject.players = players;

console.log( myObject );

which will result in:

[object Object] {
  players: [object Object] {
    player1: [object Object] { ... },
    player2: [object Object] { ... }
  },
  total: "2"
}

Object Literal needs a comma separated PROPERTY : VALUE pair model

{
   "property1" : val,
   "property2" : [],
   "property3" : "String",
   "property4" : {}
}

The above is a valid JSON cause implies that your property names are wrapped in double quotes " ".

"property" : value

Array (as seen in your Question) cannot be separated by : but with ,

myArr = ["string", {}, value]; // is a valid Array.

with object:

myArr = ["string", {"objectProperty":"val"}, value];

Now to get a key value out of Array:

myArr[0] // "string"
myArr[1] // {object Object}
myArr[1].objectProperty // "val" // get Object property value 

So as seen from above Arrays are values stored in numbered keys (0 zero based), while Objects are property - value pairs. What they have in common is the comma (,) delimiter.

Upvotes: 1

bobthedeveloper
bobthedeveloper

Reputation: 3783

Your example isn't a correct JSON object, you should do it like this

var x = { "total": "2" }
x.players = playerArray

Upvotes: 1

Tib
Tib

Reputation: 2631

Your json is not valid. If you want something valid, you need to do something like

{
  "total": "2",
  "players": [
     "player1": {
          "score": "100",
          "ping": "50"
     },
     "player2": {
          "score": "100",
          "ping": "50"
     }
  ]
}

Upvotes: 2

Related Questions