Unmarshalling Nested JSON in Golang
A Golang novice posed a query regarding the unmarshalling of complex JSON data into a structured map, specifically focusing on the unmarshalling of nested JSON objects into custom types.
Problem Statement
The JSON data in question consists of nested objects representing a story and its options. The map data structure used for unmarshalling was expected to contain structured Context and Option data types, but the resulting map only contained empty values.
Solution
The issue faced by the user stems from the lack of exported field names in their data structures. To enable proper marshalling and unmarshalling, data structure fields must be exported, signifying their accessibility outside the package.
By adding string tags to the exported field names, the correct field bindings can be established between the JSON data and the data structures. Here's the corrected version of the code:
type Context struct {
The above is the detailed content of How to Unmarshall Nested JSON Objects into Custom Types in Golang?. For more information, please follow other related articles on the PHP Chinese website!