Search code examples
c#.netjsondata-structuresjson-deserialization

Trying to deserialize a complex json file in c#


I'm trying to deserialize a json file in c# and print the values of each individual value by using the key, but for the json values that are a list, I'm unable to print the desired values. Json file:

{   
    "prod": {
        "vpc_name": "vpc-us-east-1-it-prod",
        "subnets": {
            "application": [ "subnet-1234", "subnet-1345" ],
            "data":        [ "subnet-64t3", "subnet-f321" ],
            "edge":        [ "subnet-1ff3", "subnet-134g" ]
        }
    },
    "dev": {
        "vpc_name": "vpc-us-east-1-it-dev",
        "subnets": {
            "application": [ 
                { "subnet_id": "subnet-2345tf", "az": "us-east-1a" },
                { "subnet_id": "subnet-143f1", "az": "us-east-1b" }
            ],
            "data":        [ 
                { "subnet_id": "subnet-134f", "az": "us-east-1b" },
                { "subnet_id": "subnet-1324", "az": "us-east-1a" }
            ],
            "edge":        [ 
                { "subnet_id": "subnet-123434", "az": "us-east-1a" },
                { "subnet_id": "subnet-123fg", "az": "us-east-1b" }
            ]
        }
    }
}

C# code

using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;

namespace JsonDeserializer
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            string jsonString = File.ReadAllText(@"C:/Users/Stephen.Carvalho/source\repos/JsonDeserializer/JsonDeserializer/vpc_stack.json");
            Rootobject vpcs = JsonSerializer.Deserialize<Rootobject>(jsonString);
            Console.WriteLine(vpcs.dev.vpc_name);
            Console.WriteLine(vpcs.dev.subnets.application);

        }
    }


    public class Rootobject
    {
        public Prod prod { get; set; }
        public Dev dev { get; set; }
    }

    public class Prod
    {
        public string vpc_name { get; set; }
        public Subnets subnets { get; set; }
    }

    public class Subnets
    {
        public string[] application { get; set; }
        public string[] data { get; set; }
        public string[] edge { get; set; }
    }

    public class Dev
    {
        public string vpc_name { get; set; }
        public Subnets1 subnets { get; set; }
    }

    public class Subnets1
    {
        public Application[] application { get; set; }
        public Datum[] data { get; set; }
        public Edge[] edge { get; set; }
    }

    public class Application
    {
        public string subnet_id { get; set; }
        public string az0885b3b66d { get; set; }
        public string az { get; set; }
    }

    public class Datum
    {
        public string subnet_id { get; set; }
        public string az { get; set; }
    }

    public class Edge
    {
        public string subnet_id { get; set; }
        public string az { get; set; }
    }
}

Output

Hello World!
vpc-us-east-1-it-dev
JsonDeserializer.Application[]

I want to print the list of application subnets with prod.subnets but instead of getting the values returned I'm getting JsonDeserializer.Application[] as the output


Solution

  • That is because vpcs.dev.subnets.application is an array of Application. You can print the array of application subnets by first getting the subnet_ids then converting that array to a string.

    Reason you see JsonDeserializer.Application[] is because your printing the object itself.. when you do that, you get the name of the object type instead of the values inside of it.

    Console.WriteLine(string.Join(", ", vpcs.dev.subnets.application.Select(x => x.subnet_id)));
    

    The Select statment goes through each of the application and returns only the subnet_id. Whenever you have an array / list, you can use the select method to get specific items from the list or create new object with this as well.

    Documentation on Select