Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to merge multiple JSON files which has the same type of data inside them and fetch the merged data from those JSONs. For example below are two JSON files.

JSON 1

JavaScript
[
{
    "Name": "Sample1",
    "Data": [
        {
            "Name": "Sample1 Sub1",
            "Data": [
                {
                    "Name": "XXX",
                    "ID": ["278924"]
                }
            ]
        }
    ]
},
{
    "Name": "Sample2",
    "Data": [
        {
            "Name": "Sample2 Sub1",
            "Data": [
                {
                    "Name": "XXX",
                    "ID": ["278378"]
                },
                {
                    "Name": "YYY",
                    "ID": ["278289"]
                }
            ]
        }
    ]
}
]

JSON 2

JavaScript
[
{
    "Name": "Sample1",
    "Data": [
        {
            "Name": "Sample1 Sub1",
            "Data": [
                {
                    "Name": "XXX",
                    "ID": ["357896"]
                }
            ]
        }
    ]
},
{
    "Name": "Sample2",
    "Data": [
        {
            "Name": "Sample2 Sub1",
            "Data": [
                {
                    "Name": "XXX",
                    "ID": ["356842"]
                },
                {
                    "Name": "YYY",
                    "ID": ["357123"]
                }
            ]
        }
    ]
}
]

I'm expecting the output to be in the below format.

[
{
    "Name": "Sample1",
    "Data": [
        {
            "Name": "Sample1 Sub1",
            "Data": [
                {
                    "Name": "XXX",
                    "ID": ["278924, 357896"]
                }
            ]
        }
    ]
},
{
    "Name": "Sample2",
    "Data": [
        {
            "Name": "Sample2 Sub1",
            "Data": [
                {
                    "Name": "XXX",
                    "ID": ["278378,356842"]
                },
                {
                    "Name": "YYY",
                    "ID": ["278289,357123"]
                }
            ]
        }
    ]
}
]

Any help would be appreciated.


What I have tried:

I tried to deserialize the JSON into List in Class format and group by from there which ended in multiple loops. I don't have any code handy, I'm parallely working on this now
Posted
Comments
Richard MacCutchan 26-Mar-19 13:30pm    
The easiest way must be to convert the JSON to the associated C# class objects and merge it from there. But since you have not shown any code, or explained what failures you see, it is difficult to suggest much.
Member 11405465 26-Mar-19 13:50pm    
Hi thank you for your reply. I tried to deserialize the jsons into c# class objects and applied group by which yielded me first level of grouping. Below is the sample code.

var JsonList = new List<rootobject>();
string[] files = { @"SampleJSON.json", @"C:SampleJSON1.json" };
foreach (var file in files)
{
JsonList.AddRange(JsonConvert.DeserializeObject<list<rootobject>>(File.ReadAllText(file)));
}
var JsonArrays = (JsonList.GroupBy(x => x.name).Select(y => y));

But I still have two more list inside those grouped data which again needs to be grouped.
Richard MacCutchan 27-Mar-19 4:22am    
Perform the same code for the different groups, creating new arrays/lists, and then merge the lists/arrays before serialising them back to JSON.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900