How to combine two different JSON into one JSON in C #

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

}
+4
source share
4 answers

If you don't want to mess with string inserts, you can go (and I recommend) 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});
+1
source

You can deserialize them into two objects, create a new anonymous type of these objects and serialize them at the end of 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
                               }]")
        });
+4
source
+2

try it

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

Source: https://habr.com/ru/post/1626194/


All Articles