Search code examples
c#jsonvariablessplittxt

TXT to JSON with variable split depending on first 3 characters in c#


I've been trying to look up guides and not sure what to do here. I have a large TXT file that I need to convert to JSON.

But depending on the first 3 characters of a line different rules apply to the remainder of the line.

Example case:

001010100092019051513040000000000000000201905150000Skovharevej         Skovharevej                             
001010100102019051513040000000000000000201905150000Svalehalevej        Svalehalevej  
00401010013001 001 U2018092921102150Nordhavn            
00901010013001 001 U20220114101618Hans Egedes                   
01301010013001 001 U2022011410167046Hans Egedes,Kbhvn   
01401010013001 001 U202201141016561. Øst  

If text line starts with 001, then type is a road

  1. first 3 is type code
  2. next 4 is kommune code
  3. next 4 is road code
  4. next 12 is timestamp
  5. next 4 is road to next kommune code
  6. next 4 is road to next road code
  7. next 4 is road from previous kommune code
  8. next 4 is road from previous road code
  9. Next 12 is startdate
  10. next 20 is roadname
  11. next 40 is expanded road name

Now if type is 004

  1. then first 3 is type
  2. next 4 is kommune code
  3. next 4 is road code
  4. next 4 is housenumber from
  5. next 4 is housenumber to
  6. next 1 is if housenumber is a even or uneven number
  7. next 12 is timestamp
  8. next 4 is postal number
  9. next 20 is postal district

So to reiterate based on above information.

  • I need to input a txt file in C#

  • Then read each line

      var lines = File.ReadLines(fileName); 
      foreach (var line in lines)
    
  • I then have to read the first 3 characters and upon those determine what type it is.

  • I then have to take that line through a specific rule split

  • And finally output a json file

Solution Below

Controller

public class ConvertController
{
    public void convertTxtFile()
    {
        var filename = "C:\\A370715.txt";
        var lines = File.ReadLines(filename);
        string jsonString = "";
        if (!File.Exists("C:\\path.json"))
        {
        TextWriter tsw = new StreamWriter(@"C:\path.json", true);
        foreach (var line in lines)
        {
            System.Console.WriteLine(line);
            var code = line.Substring(0, 3);
            System.Console.WriteLine($"Code: {code}");
            switch (code)
            {
                case "001":
                    Class001 c001 = new Class001(line);
                    jsonString = JsonSerializer.Serialize(c001);
                    tsw.WriteLine(jsonString);
                    System.Console.WriteLine(jsonString);
                    break;
                case "002":
                    Class002 c002 = new Class002(line);
                    jsonString = JsonSerializer.Serialize(c002);
                    tsw.WriteLine(jsonString);
                    System.Console.WriteLine(jsonString);
                    break;
                case "003":
                    Class003 c003 = new Class003(line);
                    jsonString = JsonSerializer.Serialize(c003);
                    tsw.WriteLine(jsonString);
                    System.Console.WriteLine(jsonString);
                    break;
                case "004":
                    Class004 c004 = new Class004(line);
                    jsonString = JsonSerializer.Serialize(c004);
                    tsw.WriteLine(jsonString);
                    System.Console.WriteLine(jsonString);
                    break;
                case "005":
                    Class005 c005 = new Class005(line);
                    jsonString = JsonSerializer.Serialize(c005);
                    tsw.WriteLine(jsonString);
                    System.Console.WriteLine(jsonString);
                    break;
                case "006":
                    Class006 c006 = new Class006(line);
                    jsonString = JsonSerializer.Serialize(c006);
                    tsw.WriteLine(jsonString);
                    System.Console.WriteLine(jsonString);
                    break;
                case "007":
                    Class007 c007 = new Class007(line);
                    jsonString = JsonSerializer.Serialize(c007);
                    tsw.WriteLine(jsonString);
                    System.Console.WriteLine(jsonString);
                    break;
                case "008":
                    Class008 c008 = new Class008(line);
                    jsonString = JsonSerializer.Serialize(c008);
                    tsw.WriteLine(jsonString);
                    System.Console.WriteLine(jsonString);
                    break;
                case "009":
                    Class009 c009 = new Class009(line);
                    jsonString = JsonSerializer.Serialize(c009);
                    tsw.WriteLine(jsonString);
                    System.Console.WriteLine(jsonString);
                    break;
                case "010":
                    Class010 c010 = new Class010(line);
                    jsonString = JsonSerializer.Serialize(c010);
                    tsw.WriteLine(jsonString);
                    System.Console.WriteLine(jsonString);
                    break;
                case "011":
                    Class011 c011 = new Class011(line);
                    jsonString = JsonSerializer.Serialize(c011);
                    tsw.WriteLine(jsonString);
                    System.Console.WriteLine(jsonString);
                    break;
                case "012":
                    Class012 c012 = new Class012(line);
                    jsonString = JsonSerializer.Serialize(c012);
                    tsw.WriteLine(jsonString);
                    System.Console.WriteLine(jsonString);
                    break;
                case "013":
                    Class013 c013 = new Class013(line);
                    jsonString = JsonSerializer.Serialize(c013);
                    tsw.WriteLine(jsonString);
                    System.Console.WriteLine(jsonString);
                    break;
                case "014":
                    Class014 c014 = new Class014(line);
                    jsonString = JsonSerializer.Serialize(c014);
                    tsw.WriteLine(jsonString);
                    System.Console.WriteLine(jsonString);
                    break;
                case "015":
                    Class015 c015 = new Class015(line);
                    jsonString = JsonSerializer.Serialize(c015);
                    tsw.WriteLine(jsonString);
                    System.Console.WriteLine(jsonString);
                    break;
                case "016":
                    Class016 c016 = new Class016(line);
                    jsonString = JsonSerializer.Serialize(c016);
                    tsw.WriteLine(jsonString);
                    System.Console.WriteLine(jsonString);
                    break;
                default:
                    System.Console.WriteLine($"What to do with code: {code} ?");
                    break;
            }
        }
        System.Console.WriteLine("Convert is finished");
        } else { System.Console.WriteLine("File already exist please remove file before creating a new one!"); }
    }  
}

Class

public class Class001
{
    public string type { get; set; }
    public string kommuneCode { get; set; }
    public string roadCode { get; set; }
    public string timestamp { get; set; }
    public string roadToNextKommuneCode { get; set; }
    public string roadToNextRoadCode { get; set; }
    public string roadToPreviousKommuneCode { get; set; }
    public string roadToPreviousRoadCode { get; set; }
    public string startDate { get; set; }
    public string roadName { get; set; }
    public string ExpandedRoadName { get; set; }


    public Class001(string s)
    {
        this.type = s.Substring(0, 3);
        this.kommuneCode = s.Substring(3, 4);
        this.roadCode = s.Substring(7, 4);
        this.timestamp = s.Substring(11, 12);
        this.roadToNextKommuneCode = s.Substring(23, 4);
        this.roadToNextRoadCode = s.Substring(27, 4);
        this.roadToPreviousKommuneCode = s.Substring(31, 4);
        this.roadToPreviousRoadCode = s.Substring(35, 4);
        this.startDate = s.Substring(39, 12);
        this.roadName = s.Substring(51, 20);
        this.ExpandedRoadName = s.Substring(71, 40);
    }
}

Solution

  • Your program could look like:

     static void Main(string[] args)
        {
            string input = @"001010100092019051513040000000000000000201905150000Skovharevej         Skovharevej                             
    001010100102019051513040000000000000000201905150000Svalehalevej        Svalehalevej  
    00401010013001 001 U2018092921102150Nordhavn            
    00901010013001 001 U20220114101618Hans Egedes                   
    01301010013001 001 U2022011410167046Hans Egedes,Kbhvn   
    01401010013001 001 U202201141016561. Øst  ";
            foreach (var line in input.Split("\r\n"))
            {
                System.Console.WriteLine(line);
                string code = line.Substring(0, 3);
                System.Console.WriteLine($"Code: {code}");
                switch (code)
                {
                    case "001":
                        Class001 c001 = new Class001(line);
                        string jsonString = JsonSerializer.Serialize(c001);
                        System.Console.WriteLine(jsonString);
                        break;
                    case "003":
                        //TODO: Handling of class "003"
                        break;
                    default:
                        System.Console.WriteLine($"What to do with code: {code} ?");
                        break;
                }
            }
    
        }
    

    with the Class001:

    public class Class001
    {
        public string code { get; set; }
        public string kommune { get; set; }
        public string road { get; set; }
        public string timestamp { get; set; }
    
        /// etc, etc
    
        public Class001(string s)
        {
            this.code = s.Substring(0, 3);
            this.kommune = s.Substring(3, 4);
            this.road = s.Substring(7, 4);
            this.timestamp = s.Substring(11, 12);
        }
    }
    

    This code should start with the following output:

    001010100092019051513040000000000000000201905150000Skovharevej         Skovharevej
    Code: 001
    {"code":"001","kommune":"0101","road":"0009","timestamp":"201905151304"}
    001010100102019051513040000000000000000201905150000Svalehalevej        Svalehalevej
    Code: 001
    {"code":"001","kommune":"0101","road":"0010","timestamp":"201905151304"}
    
    • For the JsonSerializer.Serialize() to work, you need to add: using System.Text.Json;

    EDIT: To create one complete JSON, you can change this code to:

    static void Main(string[] args)
        {
            string input = @"00101010009201905..............";
            List<Object> completeJSON = new List<Object>();
            foreach (var line in input.Split("\r\n"))
            {
                System.Console.WriteLine(line);
                string code = line.Substring(0, 3);
                System.Console.WriteLine($"Code: {code}");
                switch (code)
                {
                    case "001":
                        Class001 tmp_c001 = new Class001(line);
                        //string jsonString = JsonSerializer.Serialize(c001);
                        completeJSON.Add(tmp_c001);
                        break;
                    case "004":
                        Class004 tmp_c004 = new Class004(line);
                        completeJSON.Add(tmp_c004);
                        break;
                    default:
                        System.Console.WriteLine($"What to do with code: {code} ?");
                        break;
                }
            }
          System.Console.WriteLine(JsonSerializer.Serialize(completeJSON));
        }
    

    This should give something (when only considering the first two lines of your input):

    [{"code":"001","kommune":"0101","road":"0009","timestamp":"201905151304"},{"code":"001","kommune":"0101","road":"0010","timestamp":"201905151304"}]