Flexible Methods for Dynamically Parsing JSON Arrays of Objects in C#

Dec 02, 2025 · Programming · 10 views · 7.8

Keywords: C# | JSON Parsing | Dynamic Objects

Abstract: This article explores solutions for handling dynamic JSON arrays of objects in C#, focusing on using Newtonsoft.Json's JObject and JToken for recursive traversal. It details how to extract object data from JSON arrays without predefining class structures, with code examples illustrating the implementation. Additionally, it compares other common parsing methods, such as JArray.Parse and dynamic deserialization, to help developers choose appropriate techniques based on practical needs.

In C# development, processing JSON data is a common task, but when JSON structures change dynamically, predefined classes may not suffice. This article addresses a typical scenario: parsing JSON strings containing arrays of objects with variable properties, and discusses flexible data extraction methods.

Problem Background and Challenges

Consider a JSON string representing an array of bank information:

{"results":[{"SwiftCode":"","City":"","BankName":"Deutsche Bank","Bankkey":"10020030","Bankcountry":"DE"},{"SwiftCode":"","City":"10891 Berlin","BankName":"Commerzbank Berlin (West)","Bankkey":"10040000","Bankcountry":"DE"}]}

Since returned results may vary each time, static classes like Result cannot be used for deserialization. The goal is to convert the JSON array into an array of objects in C#, where each object contains the properties of the original JSON object.

Core Solution: Recursive Parsing with Newtonsoft.Json

Newtonsoft.Json (now Json.NET) is a popular library for JSON handling. The following code demonstrates how to recursively traverse the JSON tree to extract objects from the array:

using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json.Linq;

class Program
{
    static void Main()
    {
        string json = "{'results':[{'SwiftCode':'','City':'','BankName':'Deutsche Bank','Bankkey':'10020030','Bankcountry':'DE'},{'SwiftCode':'','City':'10891 Berlin','BankName':'Commerzbank Berlin (West)','Bankkey':'10040000','Bankcountry':'DE'}]}";

        var resultObjects = AllChildren(JObject.Parse(json))
            .First(c => c.Type == JTokenType.Array && c.Path.Contains("results"))
            .Children<JObject>();

        foreach (JObject result in resultObjects) {
            foreach (JProperty property in result.Properties()) {
                // Process property
                string name = property.Name;
                string value = property.Value.ToString();
                // Example: output to console
                System.Console.WriteLine($"Name: {name}, Value: {value}");
            }
        }
    }

    // Recursively yield all child tokens
    private static IEnumerable<JToken> AllChildren(JToken json)
    {
        foreach (var c in json.Children()) {
            yield return c;
            foreach (var cc in AllChildren(c)) {
                yield return cc;
            }
        }
    }
}

This method uses the AllChildren function to recursively traverse the JSON structure, employing LINQ to find tokens of type array with paths containing "results", then iterating over their child objects. This allows dynamic handling of any JSON array without hardcoding properties.

Comparison with Other Parsing Methods

Beyond the recursive approach, other techniques can be used in similar scenarios:

The recursive method balances flexibility and control, making it ideal for unknown or varying JSON schemas.

Practical Applications and Considerations

In real-world projects, consider the following factors:

  1. Performance: Recursive traversal may impact performance with large JSON; consider caching results or using asynchronous processing.
  2. Error Handling: Add exception handling, such as try-catch blocks, to manage invalid JSON formats.
  3. Library Dependency: Ensure the project references Newtonsoft.Json, installable via NuGet.
  4. Output Format: As noted in the problem, results need to be passed to PowerShell's WriteObject; ensure output is a plain array of objects without additional wrapping.

By applying the methods discussed, developers can efficiently handle dynamic JSON data, enhancing code adaptability and maintainability.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.