package utils
import (
"compress/gzip"
"encoding/json"
"errors"
"fmt"
"io"
"mime"
"gopkg.in/yaml.v3"
)
// GetJSONObject will extract an object from a specific property of another JSON object.
// Returns nil if nothing is associated to the specified key.
func GetJSONObject(jsonObject map[string]interface{}, property string) map[string]interface{} {
object := jsonObject[property]
if object != nil {
return object.(map[string]interface{})
}
return nil
// GetArrayObject will extract an array from a specific property of another JSON object.
func GetArrayObject(jsonObject map[string]interface{}, property string) []interface{} {
return object.([]interface{})
func getBody(body io.ReadCloser, contentType string, isGzip bool) (interface{}, error) {
if body == nil {
return nil, errors.New("unable to parse response: empty response body")
reader := body
if isGzip {
gzipReader, err := gzip.NewReader(reader)
if err != nil {
return nil, err
reader = gzipReader
defer reader.Close()
bodyBytes, err := io.ReadAll(reader)
err = body.Close()
var data interface{}
err = unmarshal(contentType, bodyBytes, &data)
return data, nil
func marshal(contentType string, data interface{}) ([]byte, error) {
// Note: contentType can look like: "application/json" or "application/json; charset=utf-8"
mediaType, _, err := mime.ParseMediaType(contentType)
switch mediaType {
case "application/yaml":
return yaml.Marshal(data)
case "application/json", "":
return json.Marshal(data)
return nil, fmt.Errorf("content type is not supported for marshaling: %s", contentType)
func unmarshal(contentType string, body []byte, returnBody interface{}) error {
return err
return yaml.Unmarshal(body, returnBody)
return json.Unmarshal(body, returnBody)
return fmt.Errorf("content type is not supported for unmarshaling: %s", contentType)