Search code examples
jsonvb.nettext-parsing

How can I split a txt file with JSON in it?


UPDATE

I created my custom class and now I can access the data.

The class looks like this:

Public Class GeneralList

    <JsonProperty("Key1")>
    Public Property Key1 As String()

    <JsonProperty("Cola")>
    Public Property Cola As String()

    <JsonProperty("Key2")>
    Public Property Key2 As String()

    <JsonProperty("Key3")>
    Public Property Key3 As String()

    <JsonProperty("Key4")>
    Public Property Key4 As String()

    <JsonProperty("Key5")>
    Public Property Key5 As String()

    <JsonProperty("Key6")>
    Public Property Key6 As String()
End Class

Public Class GeneralListRoot

    <JsonProperty("GeneralList")>
    Public Property  As GeneralList()
End Class

Now what I need to do is, make each item its own txt file. I'm guessing I have to re-serialize each object and write it to a txt file, but how can I do this? Something along the lines of:

Private Sub splitTXT()

    Dim ReceiptsToProcess As GeneralListRoot = JsonConvert.DeserializeObject(Of GeneralListRoot)(json)

    For i As Integer = 0 To ReceiptsToProcess.GeneralList.Length
        
        Dim str as String = JsonConvert.SerializeObject(New With {Key .data = ReceiptsToProcess.GeneralList(i)})

        'Streamwriter etc etc 
    Next

End Sub

Original Post

I have a really long .txt file that has JSON in it.

The file represents many receipts, which have the following structure, each Key1 through Key6 is a receipt:

{
    "GeneralList": [
        {
            "Key1": [
                "Company Name         NIT 1028367023",
                "Sucursal 02                                                        Fact:  68831",
                "Calle Bolivar Nro 156 Telf.:3360000 Santa Cruz-Bolivia   Nov/2020  Nro.Auto:416801000297136",
                "                        F A C T U R A                    Distr.",
                "Actividad Economica: Telecomunicaciones",
                "CI: 1516188 Telefono: 33432352                                     Cajero: 90812",
                "Nombre   : GUILLERMO ANTONIO PARADA SUAREZ                         Fecha :11-02-2021",
                "Contrato : 40000479                                                Hora  :10:02:00",
                "Direccion: CALL SIN_NOMBRE 0081                                    Lugar :Bank",
                "Ref.Pago : 33432352",
                "Fecha de Emision         Fecha Maxima de Entrega  Fecha de Vencimiento",
                "30-11-2020                 03-12-2020              03-01-2021",
                "SERVICIO: TELEFONICO               Categoria: SOCIO    Periodo : 01-11-20 al 30-11-20",
                " "
            ],
            "Cola": [
                "Fecha Pago                    ",
                "11-02-21                      ",
                "Factura                       ",
                "68831                         ",
                "Telefono                      ",
                "33432352                      ",
                "Monto                         ",
                "   150.12                     ",
                "Entidad                       ",
                "40                            ",
                "Cajero                        ",
                " 90812                        ",
                "Hora                          ",
                "10:02:00                      "
            ],
            "Key2": [
                "                                    DETALLE                      IMPORTE Bs.",
                "TARIFA BASICA SOCIO                                                               52.72",
                "REINSTALACION                                                                     27.00",
                "TASA DE REGULACION AFCOOP                                                          0.50",
                "LLAMADA EN ESPERA                                                                  0.00",
                "TELE-CONDOMINIO                                                                   34.40",
                "BLOQUEO CONTROLADO DE LLAMADAS                                                    22.30",
                "IDENTIFICADOR DE LLAMADAS                                                         13.20",
                " ",
                " ",
                " ",
                " ",
                " ",
                " ",
                " ",
                " ",
                " ",
                " ",
                " ",
                " ",
                " ",
                " ",
                " "
            ],
            "Key3": [
                "Codigo de Control:       17-9B-69-4B-8C           Total Bs.                      150.12",
                "Fecha Limite de Emision: 02-12-2020               Base para credito fiscal:      149.62"
            ],
            "Key4": [
                "Son: Ciento Cincuenta CON 12/100 Bolivianos",
                " ",
                "Mensaje: Estimado Sr.Abonado: Solicitando,en nuestras oficinas su PIN,Ud.puede contar con su detalle de llamadas en forma rapida y comoda a traves de nuestra pag.Web Cotas.com"
            ],
            "Key5": [
                "1028367023|68831|416801000297136|30/11/2020|150.12|149.62|17-9B-69-4B-8C|1516188|0.00|0.00|0.50|0.00"
            ],
            "Key6": [
                "                        ",
                "                        ",
                "                        ",
                "                        ",
                "                        "
            ]
        },
        {
            "Key1": [
                "Company Name         NIT 1028367023",
                "Sucursal 02                                                        Fact:  95423",
                "Calle Bolivar Nro 156 Telf.:3360000 Santa Cruz-Bolivia   Ene/2021  Nro.Auto:416801100312955",
                "                        F A C T U R A                    Distr.0000",
                "Actividad Economica: Telecomunicaciones",
                "CI: 2815655 Telefono: 39219275                                     Cajero: 90812",
                "Nombre   : ANA MARIA SUAREZ DE CUELLAR                             Fecha :11-02-2021",
                "Contrato : 95421287                                                Hora  :10:45:49",
                "Direccion: CALLE RECAREDO RODA 0000                                Lugar :Bank",
                "Ref.Pago : 39219275",
                "Fecha de Emision         Fecha Maxima de Entrega  Fecha de Vencimiento",
                "31-01-2021                 03-02-2021              06-03-2021",
                "SERVICIO: TELEFONICO               Categoria: USUARIO  Periodo : 01-01-21 al 31-01-21",
                " "
            ],
            "Cola": [
                "Fecha Pago                    ",
                "11-02-21                      ",
                "Factura                       ",
                "95423                         ",
                "Telefono                      ",
                "39219275                      ",
                "Monto                         ",
                "   209.00                     ",
                "Entidad                       ",
                "40                            ",
                "Cajero                        ",
                " 90812                        ",
                "Hora                          ",
                "10:45:49                      "
            ],
            "Key2": [
                "                                    DETALLE                      IMPORTE Bs.",
                "PLAN COMBAZO ESENCIAL POSTPAGO                                                   209.00",
                " ",
                " ",
                " ",
                " ",
                " ",
                " ",
                " ",
                " ",
                " ",
                " ",
                " ",
                " ",
                " ",
                " ",
                " ",
                " ",
                " ",
                " ",
                " ",
                " ",
                " "
            ],
            "Key3": [
                "Codigo de Control:       BB-D6-2A-7C              Total Bs.                      209.00",
                "Fecha Limite de Emision: 12-02-2021               Base para credito fiscal:      209.00"
            ],
            "Key4": [
                "Son: Doscientos Nueve CON 00/100 Bolivianos",
                " ",
                "Mensaje: Estimado Sr.Abonado: Solicitando,en nuestras oficinas su PIN,Ud.puede contar con su detalle de llamadas en forma rapida y comoda a traves de nuestra pag.Web Cotas.com"
            ],
            "Key5": [
                "1028367023|95423|416801100312955|31/01/2021|209.00|209.00|BB-D6-2A-7C|2815655|0.00|0.00|0.00|0.00"
            ],
            "Key6": [
                "                        ",
                "                        ",
                "                        ",
                "                        ",
                "                        "
            ]
        },
        {
            "Key1": [
                "Company Name         NIT 1028367023",
                "Sucursal 02                                                        Fact:  36582",
                "Calle Bolivar Nro 156 Telf.:3360000 Santa Cruz-Bolivia   Dic/2020  Nro.Auto:416801000306848",
                "                        F A C T U R A                    Distr.0049",
                "Actividad Economica: Telecomunicaciones",
                "CI : 1540044 Telefono: 33488769                                    Cajero: 90812",
                "Nombre   : BLANCA ELVIRA MELGAR GOMEZ                              Fecha :11-02-2021",
                "Contrato : 60071759                                                Hora  :13:54:32",
                "Direccion: CALL UV-71  J 0000                                      Lugar :Bank",
                "Ref.Pago : 33488769",
                "Fecha de Emision         Fecha Maxima de Entrega  Fecha de Vencimiento",
                "31-12-2020                 03-01-2021              03-02-2021",
                "SERVICIO: TELEFONICO               Categoria: SOCIO    Periodo : 01-12-20 al 31-12-20",
                " "
            ],
            "Cola": [
                "Fecha Pago                    ",
                "11-02-21                      ",
                "Factura                       ",
                "36582                         ",
                "Telefono                      ",
                "33488769                      ",
                "Monto                         ",
                "   451.80                     ",
                "Entidad                       ",
                "40                            ",
                "Cajero                        ",
                " 90812                        ",
                "Hora                          ",
                "13:54:32                      "
            ],
            "Key2": [
                "                                    DETALLE                      IMPORTE Bs.",
                "PLAN COMBAZO HD AVANZADO PLUS POSTP                                              429.00",
                "TASA DE REGULACION AFCOOP                                                          0.50",
                "BLOQUEO CONTROLADO DE LLAMADAS                                                    22.30",
                " ",
                " ",
                " ",
                " ",
                " ",
                " ",
                " ",
                " ",
                " ",
                " ",
                " ",
                " ",
                " ",
                " ",
                " ",
                " ",
                " ",
                " ",
                " "
            ],
            "Key3": [
                "Codigo de Control:       D4-3D-C0-83-34           Total Bs.                      451.80",
                "Fecha Limite de Emision: 13-01-2021               Base para credito fiscal:      451.30"
            ],
            "Key4": [
                "Son: Cuatrocientos Cincuenta Y Uno CON 80/100 Bolivianos",
                " ",
                "Mensaje: Estimado Sr.Abonado: Solicitando,en nuestras oficinas su PIN,Ud.puede contar con su detalle de llamadas en forma rapida y comoda a traves de nuestra pag.Web Cotas.com"
            ],
            "Key5": [
                "1028367023|36582|416801000306848|31/12/2020|451.80|451.30|D4-3D-C0-83-34|1540044|0.00|0.00|0.50|0.00"
            ],
            "Key6": [
                "                        ",
                "                        ",
                "                        ",
                "                        ",
                "                        "
            ]
        }
    ]
}

I want to split the file into valid json format.

I need each file, 3 in total in this example, to contain all of the info in between each Key1 through Key6. The file splitting must be done programatically because the user must be able save this file from the server and send it to a print spooler service.

The files can be either .txt .json or .web

This is for a vb.net web app. I need to display info from each file in a data grid, which I will figure out later.

How can I split the file?


Solution

  • NewtonSoft JsonConvert.DeserializeObject will do what you need.

    STEP ONE

    Do what @Jimi said to create your class. For my example below, I'll assume the resulting class is called PendingReceipts.

    STEP TWO

    Imports Newtonsoft.Json
    

    STEP THREE

    Here's a mock-example. You'll have to adapt it based on your own classes etc. This one single line of code magically takes all of that JSON and populates your class instance properties with all the data.

    Dim ReceiptsToProcess As PendingReceipts = JsonConvert.DeserializeObject(Of PendingReceipts)(ReceiptJSONcode)
    

    Where:

    ReceiptsToProcess is will be a new instance of the class type, that will contain the data you will need to process.

    PendingReceipts is the class type.

    ReceiptJSONcode is a string variable containing the JSON code that needs processing.

    STEP FOUR

    Iterate through the resulting data in ReceiptsToProcess. You'll need to look at the class to understand how the data's going to be structured, so you know the right way to get at it.

    In this particular case, the way your JSON is structured, you get arrays within arrays in the resulting data. What I mean is that for each Key1, the matching data indicates it's an array - i.e. every Key1 could have multiple values, so the converter creates it as an array. However in all your examples, there's only a single value given. So you need to understand your potential data before trying to code for it. Ex. What if you actually got multiple values for a Key1? Or for Key2, but not Key3? Do you understand the data?

    Have a got at it and post your code. I'll give a hand further if needed.

    UPDATE

    You've got 90% of it now. If all you want to do is extract each and just re-save it individually, it's even easier than your post. Here, you can see both looking into the data a bit, as well as how to just export each one.

    Dim res As String = ""
    Dim myjson As String = TextBox1.Text
    Dim ReceiptstoProcess As PendingReceipts = JsonConvert.DeserializeObject(Of PendingReceipts)(myjson)
    res += $"Count: {ReceiptstoProcess.GeneralList.Count}" + vbCrLf
    
    If ReceiptstoProcess.GeneralList.Count > 0 Then
    
        'We can look at individual data points:
        For i = 0 To ReceiptstoProcess.GeneralList.Count - 1
            res += vbCrLf + $"RECEIPT #{i}-------------------------" + vbCrLf
            'NOTE: You could multiple values for Key3 etc.  You might need to iterate through them.  This doesn't go to that level, but it would be simple.
            res += "Key 3:" + vbCrLf + ReceiptstoProcess.GeneralList(i).Key3(0).ToString + vbCrLf
            res += "Key 5:" + vbCrLf + ReceiptstoProcess.GeneralList(i).Key5(0).ToString + vbCrLf
        Next
        res += vbCrLf
    
        res += "----------------" + vbCrLf + vbCrLf
    
        'We can serialize each receipt separately from the rest, and then do something with it:
        For i = 0 To ReceiptstoProcess.GeneralList.Count - 1
            res += vbCrLf + $"RECEIPT #{i}-------------------------" + vbCrLf
            res += JsonConvert.SerializeObject(ReceiptstoProcess.GeneralList(i)) + vbCrLf + vbCrLf
        Next
        res += vbCrLf
    
    Else
        res += "No receipts to process"
    End If
    MsgBox(res)
    

    Biggest difference is that you have:

    Dim str as String = JsonConvert.SerializeObject(New With {Key .data = ReceiptsToProcess.GeneralList(i)})
    

    But this works for me:

    Dim str as String = JsonConvert.SerializeObject(ReceiptstoProcess.GeneralList(i))
    

    P.S. I didn't actually try what you wrote, so maybe it would work. My immediate reaction was just it seemed like some unnecessary extra code.