Randhi Rupesh
Randhi Rupesh

Reputation: 15060

how to join two different JSON into single JSON in C#

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
    }]

}

Upvotes: 7

Views: 11711

Answers (3)

Dextere
Dextere

Reputation: 301

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);

Upvotes: 0

Alex
Alex

Reputation: 8937

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
                               }]")
        });

Upvotes: 5

serhiyb
serhiyb

Reputation: 4833

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});

Upvotes: 5

Related Questions