7

My first JSON is as follows

[{
    "UserId": 4,
    "FirstName": "rupesh",
    "LastName": "Abc",
    "Email": "[email protected]",
    "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": "[email protected]",
        "Gender": "Male"
    }],
    "AccountDetails": [{
        "AccountId": 2,
        "AccountName": "rupeshinfo",
        "AccountDomain": null,
        "RoleId": 1,
        "UserId": 4
    }, {
        "AccountId": 3,
        "AccountName": "Rameshinfo",
        "AccountDomain": null,
        "RoleId": 2,
        "UserId": 4
    }]

}
3
  • 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 Commented Jan 27, 2016 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. Commented Jan 27, 2016 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. Commented Jan 27, 2016 at 15:56

3 Answers 3

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

Comments

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': '[email protected]',
                           '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
                               }]")
        });

Comments

0

Try this

var jsonStr ='{"UserDetails":[{"UserId": 4,"FirstName": "rupesh","LastName": "Abc","Email": "[email protected]","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);

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.