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
- first 3 is type code
- next 4 is kommune code
- next 4 is road code
- next 12 is timestamp
- next 4 is road to next kommune code
- next 4 is road to next road code
- next 4 is road from previous kommune code
- next 4 is road from previous road code
- Next 12 is startdate
- next 20 is roadname
- next 40 is expanded road name
Now if type is 004
- then first 3 is type
- next 4 is kommune code
- next 4 is road code
- next 4 is housenumber from
- next 4 is housenumber to
- next 1 is if housenumber is a even or uneven number
- next 12 is timestamp
- next 4 is postal number
- 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);
}
}
2
Answers
I’ll answer the question broadly because it seems you’re just asking for conceptual guidance but if you need more detail please clarify.
Basic process would be:
You could go directly from the structured data to JSON but unless performance is a huge concern it’s not worth it. Leverage the great C# JSON serializers out there. All you have to do is turn your structured data into C# objects.
Your program could look like:
with the Class001:
This code should start with the following output:
JsonSerializer.Serialize()
to work, you need to add:using System.Text.Json;
EDIT: To create one complete JSON, you can change this code to:
This should give something (when only considering the first two lines of your input):