Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

json

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

}
like image 335
Randhi Rupesh Avatar asked Jan 27 '16 14:01

Randhi Rupesh


People also ask

How do I combine multiple JSON objects into one?

JSONObject to merge two JSON objects in Java. We can merge two JSON objects using the putAll() method (inherited from interface java. util.

How do I combine two JSON responses?

We can merge two JSON arrays using the addAll() method (inherited from interface java. util.


2 Answers

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});
like image 56
serhiyb Avatar answered Nov 08 '22 23:11

serhiyb


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
                               }]")
        });
like image 33
Alex Avatar answered Nov 08 '22 22:11

Alex