Search code examples
jsongorevel

How to create this JSON with Go


I am trying to create the go code that will output JSON. The JSON string represents an invoice and its line items. There are two main parts and that is the header record and the item list. The item list is what is giving me trouble. It contains an array of invoice line items. I can't figure out how to construct this in go. Can someone help me out on this?

package controllers

import (
    "github.com/revel/revel"
)

type Test struct {
    *revel.Controller
}

type headerRecord struct {
    ClientNumber string
    BusinessUnit string
    ValidationKey string
    DataYear string
    DataMonth string
    TotalRevenue string
}

type dataRecord struct {
    LineNumber string
    InvoiceNumber string
    CustomerNumber string
    Zipcode string
    TransDate string
    Revenue string
    TaxSitusRule string
    TransTypeCode string
    SalesTypeCode string
    RegulatoryCode string
    TaxExemptionCodeList []string
    Geocode string
}

type request struct {
    headerRecord
    ItemList []dataRecord
}

func (c Test) Test() revel.Result {

    request := request{
        headerRecord: headerRecord{
            ClientNumber: "0000000001",
            BusinessUnit: "Biz Unit",
            ValidationKey: "123456",
            DataYear: "2016",
            DataMonth: "05",
            TotalRevenue: "600.00",
        },
        ItemList: []dataRecord{
        },

    }

/*
1st data record
        LineNumber: "1",
        InvoiceNumber: "123456",
        Zipcode: "75024",
        TransDate: "2016-05-15",
        Revenue: "100.00",
        TaxSitusRule: "05",
        TransTypeCode: "050201",
        SalesTypeCode: "B",
        RegulatoryCode: "99",
        TaxExemptionCodeList: []string{"00"},
        Geocode: "",
 */
    return c.RenderJSON(request)

}

This is what the output should be:

{
    "ClientNumber": "0000000001",
    "BusinessUnit": "Biz Unit",
    "ValidationKey": "12345",
    "DataYear": "2016",
    "DataMonth": "05",
    "TotalRevenue": "600.00",
    "ItemList": [
        {
            "LineNumber": "1",
            "InvoiceNumber": "123456",
            "CustomerNumber": "98765",
            "BillToNumber": "",
            "Zipcode": "75024",
            "TransDate": "2016-05-15",
            "Revenue": "100.00",
            "TaxSitusRule": "05",
            "TransTypeCode": "050201",
            "SalesTypeCode": "B",
            "RegulatoryCode": "99",
            "TaxExemptionCodeList": [
                "00"
            ],
            "Geocode": ""
        },
        {
            "LineNumber": "2",
            "InvoiceNumber": "123456",
            "CustomerNumber": "98765",
            "BillToNumber": "",
            "Zipcode": "75024",
            "TransDate": "2016-05-15",
            "Revenue": "200.00",
            "TaxSitusRule": "05",
            "TransTypeCode": "050201",
            "SalesTypeCode": "B",
            "RegulatoryCode": "99",
            "TaxExemptionCodeList": [
                "00"
            ],
            "Geocode": ""
        },
        {
            "LineNumber": "3",
            "InvoiceNumber": "123456",
            "CustomerNumber": "98765",
            "BillToNumber": "",
            "Zipcode": "75024",
            "TransDate": "2016-05-15",
            "Revenue": "300.00",
            "TaxSitusRule": "05",
            "TransTypeCode": "050201",
            "SalesTypeCode": "B",
            "RegulatoryCode": "99",
            "TaxExemptionCodeList": [
                "00"
            ],
            "Geocode": ""
        }
    ]
}

Solution

  • If you'd like to initialize it as a literal, you need to put all the commas in place:

    request := request{
        headerRecord: headerRecord{
            ClientNumber:  "0000000001",
            BusinessUnit:  "Biz Unit",
            ValidationKey: "123456",
            DataYear:      "2016",
            DataMonth:     "05",
            TotalRevenue:  "600.00",
        },
        ItemList: []dataRecord{
            {
                LineNumber:           "1",
                InvoiceNumber:        "123456",
                Zipcode:              "75024",
                TransDate:            "2016-05-15",
                Revenue:              "100.00",
                TaxSitusRule:         "05",
                TransTypeCode:        "050201",
                SalesTypeCode:        "B",
                RegulatoryCode:       "99",
                TaxExemptionCodeList: []string{"00"},
                Geocode:              "",
            },
        },
    }
    

    Please, see the working example in the Go Playground.