Created
June 27, 2018 16:28
-
-
Save vdparikh/f8f39225531fed7dc1b26390e0d9016f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package utils | |
import ( | |
"bytes" | |
"encoding/json" | |
"io" | |
) | |
// data, err := generateMap(StreamToByte(resp.Body)) | |
// GenerateMap ... Utility to allow converting a JSON to a map for output | |
func GenerateMap(inputStruct []byte) (map[string]interface{}, error) { | |
responseData := make(map[string]interface{}) | |
var data map[string]interface{} | |
err := json.Unmarshal(inputStruct, &data) | |
if err != nil { | |
return responseData, err | |
} | |
for field, val := range data { | |
// fmt.Println("Key-Value Pair: ", field, val) | |
switch val.(type) { | |
case string: | |
responseData[field] = val | |
case map[string]interface{}: | |
responseData[field] = val | |
default: | |
responseData[field] = val | |
} | |
} | |
return responseData, nil | |
} | |
// StreamToByte ... Converts IO reader to Byte | |
func StreamToByte(stream io.Reader) []byte { | |
buf := new(bytes.Buffer) | |
buf.ReadFrom(stream) | |
return buf.Bytes() | |
} | |
// StreamToString ... Converts IO reader to String | |
func StreamToString(stream io.Reader) string { | |
buf := new(bytes.Buffer) | |
buf.ReadFrom(stream) | |
return buf.String() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment