简体   繁体   中英

Reading JSON file using SSIS script component

I'm trying to read a JSON file using SSIS (script component and C# code with JSON.net library). My JSON file looks complex and I'm new to C# code. Below is the sample on how my JSON file looks like.

{
    "Product": {
        "col1": "xyz",
        "col2": "ryx"
    },
    "Samples": [{
            "col3": "read",
            "col4": "write"
        },
        {
            "col3": "read",
            "col4": "update"
        }
    ]
}

Any help would be appreciated.

You need class structure like below:

public class Product
{
    public string col1 { get; set; }
    public string col2 { get; set; }
}

public class Sample
{
    public string col3 { get; set; }
    public string col4 { get; set; }
}

public class Root
{
    public Product Product { get; set; }

    public Sample[] Samples { get; set; }
}

And below is the code you can use to read the JSON.

Note: sample.json file contains JSON response.

public static void Main(string[] args)
{

        using (var stream = new StreamReader("sample.json"))
        {

            var sample = JsonConvert.DeserializeObject<Root>(stream.ReadToEnd());
            Console.WriteLine(sample.Product.col1);
            Console.WriteLine(sample.Product.col2);
            foreach (var t in sample.Samples)
            {
                Console.WriteLine(t.col3);
                Console.WriteLine(t.col4);
            }
        }

        Console.Read();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM