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...