Jishan
Jishan

Reputation: 1684

Merging Multiple JSON Files into Single File

I am reading Multiple JSON Files as such:

using (var fbd = new FolderBrowserDialog())
{
    DialogResult result = fbd.ShowDialog();

    if (result == System.Windows.Forms.DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath))
    {
        IEnumerable<string> allJSONFiles = GetFileList("*.json", fbd.SelectedPath);
        txtOutput.Clear();
        txtOutput.Text = "Number of files found: " + allJSONFiles.ToList().Count + "\n";
        foreach (string filename in allJSONFiles)
        {
            txtOutput.Text += filename + "\n";
        }                   
    }
}

Now the JSON files more or less look like this, with multiple objects:

[{
        "Domain": "example.com",
        "A": ["50.63.202.28"],
        "MX": ["0 example-com.mail.protection.outlook.com."],
        "NS": ["ns48.example.com.", "ns47.example.com."],
        "SOA": ["ns47.example.com. dns.jomax.net. 2017062304 28800 7200 604800 600"],
        "TXT": ["\"MS=ms94763887\"", "\"google-site-verification=example-f0KFEgl-HnJF4_Gk\"", "\"v=spf1 include:spf.protection.outlook.com -all\""],
        "Country": ["United States"],
        "Hostname": ["'ip-50-63-202-28.ip.secureserver.net'"],
        "SSL": ["None"],
        "WHOIS": [1096],
        "TTL": ["568"]
    }, {
        "Domain": "example.org",
        "A": ["50.63.202.59"],
        "MX": ["30 ALT2.ASPMX.L.GOOGLE.COM.", "20 ALT1.ASPMX.L.GOOGLE.COM.", "50 ASPMX3.GOOGLEMAIL.COM.", "10 ASPMX.L.GOOGLE.COM.", "40 ASPMX2.GOOGLEMAIL.COM."],
        "NS": ["ns13.example.com.", "ns14.example.com."],
        "SOA": ["ns13.example.com. dns.jomax.net. 2016081700 28800 7200 604800 600"],
        "Country": ["United States"],
        "Hostname": ["'ip-50-63-202-59.ip.secureserver.net'"],
        "SSL": ["None"],
        "WHOIS": [5844],
        "TTL": ["569"]
    }
]

All I want to do is to join these objects from the multiple files I am reading to create a single file as output. So assuming that there were 2 exactly same files as stated above, my output file would contain:

[{
        "Domain": "example.com",
        "A": ["50.63.202.28"],
        "MX": ["0 example-com.mail.protection.outlook.com."],
        "NS": ["ns48.example.com.", "ns47.example.com."],
        "SOA": ["ns47.example.com. dns.jomax.net. 2017062304 28800 7200 604800 600"],
        "TXT": ["\"MS=ms94763887\"", "\"google-site-verification=example-f0KFEgl-HnJF4_Gk\"", "\"v=spf1 include:spf.protection.outlook.com -all\""],
        "Country": ["United States"],
        "Hostname": ["'ip-50-63-202-28.ip.secureserver.net'"],
        "SSL": ["None"],
        "WHOIS": [1096],
        "TTL": ["568"]
    }, {
        "Domain": "example.org",
        "A": ["50.63.202.59"],
        "MX": ["30 ALT2.ASPMX.L.GOOGLE.COM.", "20 ALT1.ASPMX.L.GOOGLE.COM.", "50 ASPMX3.GOOGLEMAIL.COM.", "10 ASPMX.L.GOOGLE.COM.", "40 ASPMX2.GOOGLEMAIL.COM."],
        "NS": ["ns13.example.com.", "ns14.example.com."],
        "SOA": ["ns13.example.com. dns.jomax.net. 2016081700 28800 7200 604800 600"],
        "Country": ["United States"],
        "Hostname": ["'ip-50-63-202-59.ip.secureserver.net'"],
        "SSL": ["None"],
        "WHOIS": [5844],
        "TTL": ["569"]
    }, {
        "Domain": "example.com",
        "A": ["50.63.202.28"],
        "MX": ["0 example-com.mail.protection.outlook.com."],
        "NS": ["ns48.example.com.", "ns47.example.com."],
        "SOA": ["ns47.example.com. dns.jomax.net. 2017062304 28800 7200 604800 600"],
        "TXT": ["\"MS=ms94763887\"", "\"google-site-verification=example-f0KFEgl-HnJF4_Gk\"", "\"v=spf1 include:spf.protection.outlook.com -all\""],
        "Country": ["United States"],
        "Hostname": ["'ip-50-63-202-28.ip.secureserver.net'"],
        "SSL": ["None"],
        "WHOIS": [1096],
        "TTL": ["568"]
    }, {
        "Domain": "example.org",
        "A": ["50.63.202.59"],
        "MX": ["30 ALT2.ASPMX.L.GOOGLE.COM.", "20 ALT1.ASPMX.L.GOOGLE.COM.", "50 ASPMX3.GOOGLEMAIL.COM.", "10 ASPMX.L.GOOGLE.COM.", "40 ASPMX2.GOOGLEMAIL.COM."],
        "NS": ["ns13.example.com.", "ns14.example.com."],
        "SOA": ["ns13.example.com. dns.jomax.net. 2016081700 28800 7200 604800 600"],
        "Country": ["United States"],
        "Hostname": ["'ip-50-63-202-59.ip.secureserver.net'"],
        "SSL": ["None"],
        "WHOIS": [5844],
        "TTL": ["569"]
    }
]

How do I achieve this without serializing (as the objects vary significantly and I want to retain the format somewhat) and merging? I tried NewtonSoft's merge, but it seems too complicated for such simple operation. Is string manipulation my last option (somehow that doesn't feel correct, hence this question)?

Note: While the example just shows 2 files, I will be merging atleast 100 files in a go.

Upvotes: 1

Views: 5347

Answers (2)

Legion
Legion

Reputation: 791

I'm not sure i understand what you mean, but seems that all the data you need is just into an "array-like" format, so using string manipulation you only need to do something like that:

Read First file (usa a for with a counter that let you know what is the first file), replace last charcater with "," and add the whole 2nd file replacing first character with " " and last character with ",".

Do the stuff of the 2nd file for all your files, when you finish replace last character with "]" and you've a single array of json stuff that can be read by a json serializer.

The cose should be something like that (write at the moment on notepad, without visual studio opened, so it could contain some digit error):

string my_big_json = string.empty;
for (int a = 0; a < allJSONFiles.Lenght; a++)
{
    StreamReader sr = new StreamReader(allJSONFiles[a]);
    string temp_data = sr.ReadToEnd().TrimEnd().TrimStart();
    sr.Close();
    if(a = 0)
    {
        temp_data = temp_data.Substring(0,temp_data.Lenght -1) + ",";
    }
    else
    {
        temp_data = temp_data.Substring(1,temp_data.Lenght -1) + " ";
        if(a < allJSONFiles.LenghtallJSONFiles.Lenght -1)
            temp_data = temp_data.Substring(0,temp_data.Lenght -1) + ",";
    }
    my_big_json += temp_data;
} 

Upvotes: 0

tym32167
tym32167

Reputation: 4881

We will need to have function which will clear brackets

private string RemoveBrackets(string content)
{
    var openB = content.IndexOf("[");
    content = content.Substring(openB + 1, content.Length - openB - 1);

    var closeB = content.LastIndexOf("]");
    content = content.Substring(0, closeB);

    return content;
}

function to merge jsons

private string MergeJsons(string[] jsons)
{
    var sb = new StringBuilder();

    sb.AppendLine("[");     
    for(var i=0; i<jsons.Length; i++)   
    {
        var json = jsons[i];
        var cleared = RemoveBrackets(json);
        sb.AppendLine(cleared);
        if (i != jsons.Length-1) sb.Append(",");
    }

    sb.AppendLine("]");     
    return sb.ToString();
}

sample data

string fileContent = @"[{
    ""Domain"": ""example.com"",
    ""A"": [""50.63.202.28""],
    ""MX"": [""0 example-com.mail.protection.outlook.com.""],
    ""NS"": [""ns48.example.com."", ""ns47.example.com.""],
    ""SOA"": [""ns47.example.com. dns.jomax.net. 2017062304 28800 7200 604800 600""],
    ""TXT"": [""\""MS=ms94763887\"""", ""\""google-site-verification=example-f0KFEgl-HnJF4_Gk\"""", ""\""v=spf1 include:spf.protection.outlook.com -all\""""],
    ""Country"": [""United States""],
    ""Hostname"": [""'ip-50-63-202-28.ip.secureserver.net'""],
    ""SSL"": [""None""],
    ""WHOIS"": [1096],
    ""TTL"": [""568""]
}
]";

Example of usage

var files = Enumerable.Range(1, 3).Select(x=>fileContent).ToArray();        
Console.WriteLine(MergeJsons(files));

Results

[
{
        "Domain": "example.com",
        "A": ["50.63.202.28"],
        "MX": ["0 example-com.mail.protection.outlook.com."],
        "NS": ["ns48.example.com.", "ns47.example.com."],
        "SOA": ["ns47.example.com. dns.jomax.net. 2017062304 28800 7200 604800 600"],
        "TXT": ["\"MS=ms94763887\"", "\"google-site-verification=example-f0KFEgl-HnJF4_Gk\"", "\"v=spf1 include:spf.protection.outlook.com -  all\""],
        "Country": ["United States"],
        "Hostname": ["'ip-50-63-202-28.ip.secureserver.net'"],
        "SSL": ["None"],
        "WHOIS": [1096],
        "TTL": ["568"]
    }  
,{
        "Domain": "example.com",
        "A": ["50.63.202.28"],
        "MX": ["0 example-com.mail.protection.outlook.com."],
        "NS": ["ns48.example.com.", "ns47.example.com."],
        "SOA": ["ns47.example.com. dns.jomax.net. 2017062304 28800 7200 604800 600"],
        "TXT": ["\"MS=ms94763887\"", "\"google-site-verification=example-f0KFEgl-HnJF4_Gk\"", "\"v=spf1 include:spf.protection.outlook.com -  all\""],
        "Country": ["United States"],
        "Hostname": ["'ip-50-63-202-28.ip.secureserver.net'"],
        "SSL": ["None"],
        "WHOIS": [1096],
        "TTL": ["568"]
    }  
,{
        "Domain": "example.com",
        "A": ["50.63.202.28"],
        "MX": ["0 example-com.mail.protection.outlook.com."],
        "NS": ["ns48.example.com.", "ns47.example.com."],
        "SOA": ["ns47.example.com. dns.jomax.net. 2017062304 28800 7200 604800 600"],
        "TXT": ["\"MS=ms94763887\"", "\"google-site-verification=example-f0KFEgl-HnJF4_Gk\"", "\"v=spf1 include:spf.protection.outlook.com -  all\""],
        "Country": ["United States"],
        "Hostname": ["'ip-50-63-202-28.ip.secureserver.net'"],
        "SSL": ["None"],
        "WHOIS": [1096],
        "TTL": ["568"]
    }  
]

Upvotes: 5

Related Questions