Help with nested json parsing


(Kevin Hoyt) #1

Hi,

I’m working on parsing out nested json data and I’m seeing an odd result. I’m getting the product description, but not the product name or location in print statements. Any help would be appreciated.

tempDevs := myDevs.DevInstances
	for _, dev := range tempDevs {
		fmt.Printf("serialNumber = %s\n",dev.SerialNumber)
		fmt.Printf("ProductName = %s\n",dev.Product.PName)
		fmt.Printf("ProductNumber = %s\n",dev.Product.PNumber)
		fmt.Printf("ProductDescription = %s\n",dev.Product.PDescription)
		fmt.Printf("Location = %s\n",dev.Product.PLocation)
		fmt.Printf("\n")
	}

here is a sample of the formatting:
‘’’{“totalRecords”:2,“instances”:[{“serialNumber”:“testSN1”,“parentSerialNumber”:“anotherSN”,“minor”:false,“instanceNumber”:“8765345678”,“installedBaseStatus”:“Latest-INSTALLED”,“endCustomer”:{“id”:“1”,“name”:“test client”,“address”:{“address1”:“100 myStreet”,“city”:“Albany”,“country”:“US”,“state”:“NY”,“postalCode”:“12345”}},“serviceSKU”:“testSKU1”,“serviceLevel”:“SL1”,“serviceDescription”:“testdesc1”,“startDate”:“2018-04-09T00:00:00Z”,“endDate”:“2019-04-08T00:00:00Z”,“product”:{“number”:“productNumber1”,“description”:“test product desc”,“family”:“fam1”,“group”:“group1”,“location”:“80616”,“name”:“test product name”},
‘’’

here are the structs I’m building out:
‘’’
type DeviceInstances struct {
SerialNumber string json:"serialNumber"
ParentSerialNumber string json:"parentSerialNumber"
Minor bool json:"minor"
InstanceNumber string json:"instanceNumber"
InstalledBaseStatus string json:"installedBaseStatus"
ServiceSKU string json:"serviceSKU"
ServiceLevel string json:"serviceLevel"
ServiceDescription string json:"serviceDescription"
StartDate string json:"startDate"
EndDate string json:"endDate"
MacId string json:"macId"
CartonId string json:"cartonId"
Quantity int json:"quantity"
SalesOrderNumber string json:"salesOrderNumber"
PurchaseOrderNumber string json:"purchaseOrderNumber"
MaintenanceSalesOrderNumber string json:"maintenanceSalesOrderNumber"
MaintenancePurchaseOrderNumber string json:"maintenancePurchaseOrderNumber"
ItemType string json:"itemType"
ShipDate string json:"shipDate"
Product DeviceProduct json:"product"
}

type DeviceProduct struct {
PName string json:"name"
PNumber string json:"number"
PDescription string json:"description"
PLocation string json:"location"
}
‘’’


(Johan Dahl) #2

Hi

I used https://mholt.github.io/json-to-go/ to generate this struct from your json

{"totalRecords":2,"instances":[{"serialNumber":"testSN1","parentSerialNumber":"anotherSN","minor":false,"instanceNumber":"8765345678","installedBaseStatus":"Latest-INSTALLED","endCustomer":{"id":"1","name":"test client","address":{"address1":"100 myStreet","city":"Albany","country":"US","state":"NY","postalCode":"12345"}},"serviceSKU":"testSKU1","serviceLevel":"SL1","serviceDescription":"testdesc1","startDate":"2018-04-09T00:00:00Z","endDate":"2019-04-08T00:00:00Z","product":{"number":"productNumber1","description":"test product desc","family":"fam1","group":"group1","location":"80616","name":"test product name"}}]}


type AutoGenerated struct {
	TotalRecords int `json:"totalRecords"`
	Instances    []struct {
		SerialNumber        string `json:"serialNumber"`
		ParentSerialNumber  string `json:"parentSerialNumber"`
		Minor               bool   `json:"minor"`
		InstanceNumber      string `json:"instanceNumber"`
		InstalledBaseStatus string `json:"installedBaseStatus"`
		EndCustomer         struct {
			ID      string `json:"id"`
			Name    string `json:"name"`
			Address struct {
				Address1   string `json:"address1"`
				City       string `json:"city"`
				Country    string `json:"country"`
				State      string `json:"state"`
				PostalCode string `json:"postalCode"`
			} `json:"address"`
		} `json:"endCustomer"`
		ServiceSKU         string    `json:"serviceSKU"`
		ServiceLevel       string    `json:"serviceLevel"`
		ServiceDescription string    `json:"serviceDescription"`
		StartDate          time.Time `json:"startDate"`
		EndDate            time.Time `json:"endDate"`
		Product            struct {
			Number      string `json:"number"`
			Description string `json:"description"`
			Family      string `json:"family"`
			Group       string `json:"group"`
			Location    string `json:"location"`
			Name        string `json:"name"`
		} `json:"product"`
	} `json:"instances"`
}


(Johan Dahl) #3

Your code seems ok. But the json you provided is not correct it misses out }]} on the end but this can’t be the error because decoding this would have been an error in go. If you print all of your struct


fmt.Printf("%#v\n", dev)

is there any other fields missing?


(Kevin Hoyt) #4

Hi Johan,

yup, I hadn’t seen that tool before, that is awesome!. It was user error. The actual data is much larger and as I was reading it to build my struct I had added fields from the shipto into the product stanza directly. I used the tool you provided and adjusted my code and it’s working properly. Thank you for the help!

Thanks,

Kevin