-1

I have an array like this:

message_list = [
    {
        "Main_body": "test msg",
        "emp_name": "test",
        "emp_salary": "5000 USD"
    }
]

Its length (e.g., message_list.length) is 1.

Now I have another array like this:

added_user_data = [
    {
        "PostSharedLog": {
            userids: "2,4,5"
        },
        "created": "2123123"
    }, 
    {
        "PostSharedLog": {
            userids: "3,1"
        },
        "created": "2123147"
    }
]

Its length (added_user_data.length) is 2.

I want to push the above array added_user_data into message_list.

I tried doing it inside a for loop, now since the length of message_list is 1, for the second iteration, the values do not get pushed since message_list[1] does not exist and is undefined. But I still want to create message_list[1] even if it does not exist.

if i do the below :

for(var k =0;<count(added_user_data.length);k++)
{
      message_list[k].PostSharedLog = added_user_data[k].PostSharedLog ; 
}

It only appends the first row from the array added_user_data and gives an output like this when i print out message_list :

[{ 
        "Main_body": "test msg",
        "emp_name": "test",
        "emp_salary": "5000 USD", 
        "PostSharedLog": {
            userids: "2,4,5"
        } }]

The second iteration does not append anything at all .

I would like to see the below :

[{ 
            "Main_body": "test msg",
            "emp_name": "test",
            "emp_salary": "5000 USD", 
            "PostSharedLog": {
                userids: "2,4,5"
            },
            "PostSharedLog": {
                userids: "3,1"
            } 

 }]
slim shady
  • 11
  • 4
  • 2
    There's no JSON here. JSON is a textual notation for data exchange. If you're dealing with source code, and not dealing with a *string*, you're not dealing with JSON. – T.J. Crowder Nov 19 '15 at 07:21
  • *"But I still want to create message_list[1] even if it does not exist."* What rules apply to that? Why stop at adding one? What tells you you shouldn't add two? What final result do you want to see? – T.J. Crowder Nov 19 '15 at 07:26
  • 1
    Also, this array is not "multi-dimensional". –  Nov 19 '15 at 07:30
  • @T.J.Crowder - i have edited my question to add the output array and what i have tried . – slim shady Nov 19 '15 at 08:43
  • @slimshady: What you've posted at the end cannot be created. You have an array with one entry in it, which is an object; that object has two properties with the same name. You can't do that, you can only have one property with a given name in an object. Your object initializer above would parse, but you'd end up with an object with just the latter `PostSharedLog` property. – T.J. Crowder Nov 19 '15 at 08:57
  • (BTW, I didn't vote to close as a duplicate. I don't see how this is a duplicate of that question. But then, I don't think the question can currently be answered, either, so...) – T.J. Crowder Nov 19 '15 at 08:58
  • @T.J.Crowder - Thanks for the expertise. Appreciate . Will have to find another way of achieving this i guess . :-( – slim shady Nov 19 '15 at 09:13

1 Answers1

0

JSON objects can not have duplicate keys.

A JSON object is basically a Map it has keys and values. Each key in an object must be unique.

Invalid JSON

What you posted as your desired outcome can never exist because PostSharedLog can only exist once. Setting it again just changes the value.

Also userids is not a valid JSON key, it should be wrapped with " always!

Any JSON linter will show you exactly why this is.

{ 
    "Main_body": "test msg",
    "emp_name": "test",
    "emp_salary": "5000 USD", 
    "PostSharedLog": {
        userids: "2,4,5"
    },
    "PostSharedLog": {
        userids: "3,1"
    } 
}

What you probably need to do is something like:

{ 
    "MainBody": "test msg",
    "EmployeeName": "test",
    "EmployeeSalary": { "amount": 5000, "currency": "USD" }, 
    "PostSharedLog": {
        "UserIds": [2,4,5,3,1]
    }
}

NOTES:

  1. Your names are not consistent. Really? Every one of them is a different format?
  2. Do not abbreviate.
  3. Do not store multivalue fields in String types, use a real Array! [1,2,3,4] instead of "1,2,3,4"
  4. Do not store mutlivalue fields in String types, use a real Object! { amount: 5000, currency: "USD" } instead of "5000 USD"