xmlpath.MustCompile

Hello guys,

I need to grab the “serial number” information from this JSON ->

{“serialNumberResponse”:{“responseHeader”:{“createdDateTime”:“2020-05-25T05:30:38.123Z”,“result”:“SUCCESS”,“uuid”:“8043fa62-dc5f-4758-b7c0-0f8159195584”,“totalPages”:“1”,“currentPage”:“1”},“serialDetails”:{“salesOrderNumber”:“109845171”,“webOrderID”:“86534410”,“purchaseOrderNumber”:“XaaS- New Backup NME”,“lines”:[{“lineNumber”:“2.1”,“partNumber”:“QSFP-H40G-AOC2M=”,“quantity”:“2”,“shipSetNumber”:“2”,“serialNumbers”:[{“serialNumber”:“FIW234802P7”},{“serialNumber”:“FIW234802PL”}]},{“lineNumber”:“4.1”,“partNumber”:“QSFP-H40G-AOC3M=”,“quantity”:“2”,“shipSetNumber”:“4”,“serialNumbers”:[{“serialNumber”:“FIW235002BS”},{“serialNumber”:“FIW235002TC”}]},{“lineNumber”:“1.1”,“partNumber”:“UCSC-C240-M5SX”,“quantity”:“1”,“shipSetNumber”:“1”,“serialNumbers”:[{“serialNumber”:“WZP234501F9”}]},{“lineNumber”:“1.1.12”,“partNumber”:“UCSC-RAID-M5HD”,“quantity”:“1”,“shipSetNumber”:“1”,“serialNumbers”:[{“serialNumber”:“LSK234801Y5”,“parentSerialNumber”:“WZP234501F9”}]},{“lineNumber”:“1.1.10”,“partNumber”:“UCS-MR-X64G2RT-H”,“quantity”:“4”,“shipSetNumber”:“1”,“serialNumbers”:[{“serialNumber”:“W0AG00094703AEB8C8”,“parentSerialNumber”:“WZP234501F9”},{“serialNumber”:“W0E300094503AABB72”,“parentSerialNumber”:“WZP234501F9”},{“serialNumber”:“W0E300094503AABD0A”,“parentSerialNumber”:“WZP234501F9”},{“serialNumber”:“W0E300094503AABD1C”,“parentSerialNumber”:“WZP234501F9”}]},{“lineNumber”:“1.1.14”,“partNumber”:“UCSC-MLOM-C40Q-03”,“quantity”:“1”,“shipSetNumber”:“1”,“serialNumbers”:[{“serialNumber”:“FCH23447G0U”,“parentSerialNumber”:“WZP234501F9”}]},{“lineNumber”:“1.1.26”,“partNumber”:“UCS-HD12T10NK9”,“quantity”:“4”,“shipSetNumber”:“1”,“serialNumbers”:[{“serialNumber”:“WFK4KE6Y”,“parentSerialNumber”:“WZP234501F9”},{“serialNumber”:“WFK4VZ2J”,“parentSerialNumber”:“WZP234501F9”},{“serialNumber”:“WFK4W01Q”,“parentSerialNumber”:“WZP234501F9”},{“serialNumber”:“WFK4W0CS”,“parentSerialNumber”:“WZP234501F9”}]},{“lineNumber”:“1.1.27”,“partNumber”:“UCS-HD24T10NK9”,“quantity”:“20”,“shipSetNumber”:“1”,“serialNumbers”:[{“serialNumber”:“WBM0JXMJ”,“parentSerialNumber”:“WZP234501F9”},{“serialNumber”:“WBM0JXN0”,“parentSerialNumber”:“WZP234501F9”},{“serialNumber”:“WBM0JXW4”,“parentSerialNumber”:“WZP234501F9”},{“serialNumber”:“WBM0JXW6”,“parentSerialNumber”:“WZP234501F9”},{“serialNumber”:“WBM1GFEV”,“parentSerialNumber”:“WZP234501F9”},{“serialNumber”:“WBM1J0AT”,“parentSerialNumber”:“WZP234501F9”},{“serialNumber”:“WBM1J0Q6”,“parentSerialNumber”:“WZP234501F9”},{“serialNumber”:“WBM1J0Y5”,“parentSerialNumber”:“WZP234501F9”},{“serialNumber”:“WBM1J13M”,“parentSerialNumber”:“WZP234501F9”},{“serialNumber”:“WBM1J188”,“parentSerialNumber”:“WZP234501F9”},{“serialNumber”:“WBM1J1DT”,“parentSerialNumber”:“WZP234501F9”},{“serialNumber”:“WBM1J1HA”,“parentSerialNumber”:“WZP234501F9”},{“serialNumber”:“WBM1J1KK”,“parentSerialNumber”:“WZP234501F9”},{“serialNumber”:“WBM1J1KM”,“parentSerialNumber”:“WZP234501F9”},{“serialNumber”:“WBM1J1KW”,“parentSerialNumber”:“WZP234501F9”},{“serialNumber”:“WBM1J1KZ”,“parentSerialNumber”:“WZP234501F9”},{“serialNumber”:“WBM1J1MH”,“parentSerialNumber”:“WZP234501F9”},{“serialNumber”:“WBM1J1R4”,“parentSerialNumber”:“WZP234501F9”},{“serialNumber”:“WBM1J1SC”,“parentSerialNumber”:“WZP234501F9”},{“serialNumber”:“WBM1J1WW”,“parentSerialNumber”:“WZP234501F9”}]},{“lineNumber”:“1.1.16”,“partNumber”:“UCSC-PSU1-1050W”,“quantity”:“2”,“shipSetNumber”:“1”,“serialNumbers”:[{“serialNumber”:“APS233701NZ”,“parentSerialNumber”:“WZP234501F9”},{“serialNumber”:“APS233701P1”,“parentSerialNumber”:“WZP234501F9”}]},{“lineNumber”:“1.1.24”,“partNumber”:“UCSC-PCI-1-C240M5”,“quantity”:“1”,“shipSetNumber”:“1”,“serialNumbers”:[{“serialNumber”:“WZP23430E64”,“parentSerialNumber”:“WZP234501F9”}]}]}}}

When I use ioutil.ReadAll and I transform the JSON output in the string I can see the full output on the (result) as following:

// result, err := ioutil.ReadAll(resp.Body)
// if err != nil {
// log.Fatal(err)
// }

// fmt.Println(string(result))

However when I use the xmlpath.MustCompile I am not able to see the JSON, even using (*) my idea was to use

path = xmlpath.MustCompile("/serialNumberResponse/serialDetails/lines//serialNumbers//serialNumber") to return only the serialNumbers

// -----------------------------------------------------------------------------------------------------------------

// /serialNumberResponse/serialDetails/lines//serialNumbers//serialNumber

path = xmlpath.MustCompile("*")
root, err = xmlpath.Parse(resp.Body)

if err != nil {
log.Fatal(err)
}
iter = path.Iter(root)
for iter.Next() {
fmt.Println(“Found:”, iter.Node().String())
}

Do you have any suggestion?

Regards
RG

The xmlpath module uses XPath to parse XML, not JSON. It is strange that the err returned by xmlpath.Parse is nil.

Hello @lutzhorn thank you for your reply.

Do you know what I need to use to resolve this problem?

Regards
RG

@rmarcandier Perhaps you’re looking for a Go implementation of “JSONPath?” https://godoc.org/github.com/yalp/jsonpath