7

My first JSON is as follows

[{
    "UserId": 4,
    "FirstName": "rupesh",
    "LastName": "Abc",
    "Email": "abc@gmail.com",
    "Gender": "Male"
}]

My Second JSON is as follows

 [{
    "AccountId": 2,
    "AccountName": "rupeshinfo",
    "AccountDomain": null,
    "RoleId": 1,
    "UserId": 4
}, {
    "AccountId": 3,
    "AccountName": "Rameshinfo",
    "AccountDomain": null,
    "RoleId": 2,
    "UserId": 4
}]

the result must be

{
    "UserDetails": [{
        "UserId": 4,
        "FirstName": "rupesh",
        "LastName": "Abc",
        "Email": "abc@gmail.com",
        "Gender": "Male"
    }],
    "AccountDetails": [{
        "AccountId": 2,
        "AccountName": "rupeshinfo",
        "AccountDomain": null,
        "RoleId": 1,
        "UserId": 4
    }, {
        "AccountId": 3,
        "AccountName": "Rameshinfo",
        "AccountDomain": null,
        "RoleId": 2,
        "UserId": 4
    }]

}
Randhi Rupesh
  • 14,650
  • 9
  • 27
  • 46
  • 3
    JSON is really a string so insert it in the string - or in the object if you are working at that level in the c# after the parse – Mark Schultheiss Jan 27 '16 at 14:14
  • @MarkSchultheiss : json is really json, it's certainly not "just a string". "Just a string" does not have things like a grammar, a specific semantic or composition rules. Sure, you can use string manipulation to manipulate your json data. Just as you can use bitwise operations to perform integer (or heaven forbids, floating point!) arithmetics. You can, but you really should not. – Falanwe Jan 27 '16 at 15:11
  • Yes but from a manipulation standpoint is what I meant; you do as you say still need to address those OTHER things regarding the contract of the notation specification at the same time; My main point here is that it is nearly always best to use a parsed object and then work with it there. And I was not clear about that - sorry for that. – Mark Schultheiss Jan 27 '16 at 15:56

3 Answers3

5

If you don't want to mess with string inserts you can go with (and I recommend so) using dynamic objects:

            var javaScriptSerializer = new JavaScriptSerializer();
            var userDetails = javaScriptSerializer.DeserializeObject(json1);
            var accountDetails = javaScriptSerializer.DeserializeObject(json2);

            var resultJson =  javaScriptSerializer.Serialize(new {UserDetails = userDetails, AccountDetails = accountDetails});
serhiyb
  • 4,753
  • 2
  • 15
  • 24
5

You can deserialize them into two objects, create new anonimous type of these objects, and serialize them into the end one json:

        JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
        var result = jsonSerializer.Serialize(new
        {
            UserDetails = jsonSerializer.DeserializeObject(@"[{
                           'UserId': 4,
                           'FirstName': 'rupesh',
                           'LastName': 'Abc',
                           'Email': 'abc@gmail.com',
                           'Gender': 'Male'
                           }]"),
            AccountDetails = jsonSerializer.DeserializeObject(@"[{
                              'AccountId': 2,
                              'AccountName': 'rupeshinfo',
                              'AccountDomain': null,
                              'RoleId': 1,
                              'UserId': 4
                              }, {
                              'AccountId': 3,
                              'AccountName': 'Rameshinfo',
                              'AccountDomain': null,
                              'RoleId': 2,
                              'UserId': 4
                               }]")
        });
Alex
  • 8,827
  • 3
  • 42
  • 58
0

Try this

var jsonStr ='{"UserDetails":[{"UserId": 4,"FirstName": "rupesh","LastName": "Abc","Email": "abc@gmail.com","Gender": "Male"}]}'

var obj = JSON.parse(jsonStr);
obj['AccountDetails'].push({"AccountId": 2,"AccountName": "rupeshinfo","AccountDomain": null,"RoleId": 1,"UserId": 4}, {"AccountId": 3,"AccountName": "Rameshinfo","AccountDomain": null,"RoleId": 2,"UserId": 4});
jsonStr = JSON.stringify(obj);
Dextere
  • 281
  • 1
  • 3
  • 13