Encoding without defining structs
Basic data types, slices, and maps can be used to encode JSON data.
How to do it...
- Use a map to represent JSON objects:
config:=map[string]any{ Â Â "ver": "1.0", Â Â "Name": "config", Â Â "type": "example", Â Â } data, err:=json.Marshal(config) // `{"ver":"1.0","Name":"config","type":"example"}` - Use a slice to represent JSON arrays:
numbersWithNil:=[]any{ 1, 2, nil, 3 } data, err:=json.Marshal(numbersWithNil) // `[1,2,null,3]` - Match the desired JSON structure to Go equivalents:
configurations:=map[string]map[string]any { Â Â "cfg1": { Â Â Â Â Â "ver": "1.0", Â Â Â Â Â "Name": "config1", Â Â }, Â Â "cfg2": { Â Â Â Â Â "ver": "1.1...