skip to Main Content

In a file path I have a few json files and I’m trying to consolidate them into one file using C# newtonsoft json.

JSON1:

[{
        "name": "name1",
        "salary": 65000
    }, {
        "name": "name2",
        "salary": 68000
    }
]

JSON2:

[{
        "name": "name3",
        "salary": 56000
    }, {
        "name": "name4",
        "salary": 58000
    }
]

JSON3:

[{
        "name": "name5",
        "salary": 76000
    }, {
        "name": "name6",
        "salary": 78000
    }
]

JSON4:

[{
        "name": "name7",
        "salary": 86000
    }, {
        "name": "name8",
        "salary": 88000
    }
]

When I use the below code in C# I get the resulting file like below.

Code:

        // List to store all the json objects
        List<object> combinedJsonObjects = new List<object>();

        // Load and append the JSON files
        foreach (string filePath in jsonFilePaths)
        {
            string jsonContent = File.ReadAllText(filePath);
            object jsonObject = JsonConvert.DeserializeObject(jsonContent);
            combinedJsonObjects.Add(jsonObject);
        }

        // Serialize the appended JSON objects
        string combinedJson = JsonConvert.SerializeObject(combinedJsonObjects, Newtonsoft.Json.Formatting.Indented);

        // save JSON file
        string combinedJsonFilePath = @"C:filePathnew.json";
        File.WriteAllText(combinedJsonFilePath, combinedJson);

Result:

[
    [{
            "name": "name1",
            "salary": 65000
        }, {
            "name": "name2",
            "salary": 68000
        }
    ],
    [{
            "name": "name3",
            "salary": 56000
        }, {
            "name": "name4",
            "salary": 58000
        }
    ],
    [{
            "name": "name5",
            "salary": 76000
        }, {
            "name": "name6",
            "salary": 78000
        }
    ],
    [{
            "name": "name7",
            "salary": 86000
        }, {
            "name": "name8",
            "salary": 88000
        }
    ]
]

But, When the multiple json files are consolidated I want the file to look like below.

Desired Result:

[{
        "name": "name1",
        "salary": 65000
    }, {
        "name": "name2",
        "salary": 68000
    }, {
        "name": "name3",
        "salary": 56000
    }, {
        "name": "name4",
        "salary": 58000
    }, {
        "name": "name5",
        "salary": 76000
    }, {
        "name": "name6",
        "salary": 78000
    }, {
        "name": "name7",
        "salary": 86000
    }, {
        "name": "name8",
        "salary": 88000
    }
]

2

Answers


  1. Try working with collection of JObject‘s:

    List<JObject> combinedJsonObjects = new List<JObject>();
    
    // Load and append the JSON files
    foreach (string filePath in jsonFilePaths)
    {
        string jsonContent = File.ReadAllText(filePath);
        object jsonObject = JsonConvert.DeserializeObject<JObject[]>(jsonContent);
        combinedJsonObjects.AddRange(jsonObject);
    }
    
    // Serialize the appended JSON objects
    string combinedJson = JsonConvert.SerializeObject(combinedJsonObjects, Newtonsoft.Json.Formatting.Indented);
    
    // save JSON file
    string combinedJsonFilePath = @"C:filePathnew.json";
    File.WriteAllText(combinedJsonFilePath, combinedJson);
    

    Otherwise serializer will procees the whole JSON tree as a new element of the resulting collection (as you see in the resulting output).

    Login or Signup to reply.
  2. @GuruStron’s answer is correct.

    Another approach is you can read each JSON file and convert it as JArray (JArray.Parse(jsonContent)) and combine the JArrays into a single array via JArray.Merge().

    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;
    
    List<object> combinedJsonObjects = new List<object>();
    JArray jArray = new JArray();
    
    // Load and append the JSON files
    foreach (string filePath in jsonFilePaths)
    {
        string jsonContent = File.ReadAllText(filePath);
        jArray.Merge(JArray.Parse(jsonContent));
    }
    
    // Serialize the appended JSON objects
    string combinedJson = JsonConvert.SerializeObject(jArray, Newtonsoft.Json.Formatting.Indented);
    
    // save JSON file
    string combinedJsonFilePath = @"C:filePathnew.json";
    File.WriteAllText(combinedJsonFilePath, combinedJson);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search