2

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"
          }
     ]
}
3
  • 1
    Your final result is invalid JSON/JavaScript. Commented Apr 13, 2014 at 19:24
  • 3
    This is NOT a valid JSON format, and you ARRAY is an invalid Array Commented Apr 13, 2014 at 19:24
  • the what about this section in your question is not a valid object cause it's missing a property name. Commented Apr 13, 2014 at 19:32

3 Answers 3

2

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"
     }
  ]
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

Thanks for the link and detailed explanation!
1

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

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

Comments

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.