-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson.go
126 lines (120 loc) · 4.43 KB
/
json.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Package guti contains utility functions
package guti
import (
"encoding/json"
"io/ioutil"
)
// JSONToMap takes a JSON byte array as input and returns a map containing the parsed JSON data.
// The method uses the json.Unmarshal function to parse the input data into a map with string keys
// and interface{} values. If the input data is not a valid JSON string, the method returns an error.
//
// This method can be useful when you need to convert a JSON string into a map of key-value pairs
// dynamically, such as when you are working with data that is generated or received in JSON format.
//
// Example usage:
//
// data := []byte(`{"name": "John Doe", "age": 30}`)
// result, err := JSONToMap(data)
// fmt.Println(result["name"]) // prints "John Doe"
// fmt.Println(result["age"]) // prints 30
//
// Playground: https://go.dev/play/p/Tcecnf8wlEb
func JSONToMap(data []byte) (map[string]interface{}, error) {
var result map[string]interface{}
err := json.Unmarshal(data, &result)
return result, err
}
// JSONToString takes a JSON object as input and returns a string representation of the JSON.
// The method uses the json.Marshal function to serialize the input data into a byte array,
// and then converts the byte array to a string.
//
// This method can be useful when you need to convert a JSON object into a string dynamically,
// such as when you are working with data that needs to be serialized or sent over the network
// as a string.
//
// Example usage:
//
// data := map[string]interface{}{"name": "John", "age": 30}
// result, err := JSONToString(data)
// fmt.Println(result) // prints '{"age":30,"name":"John"}'
//
// Playground: https://go.dev/play/p/PFc88pqp3CD
func JSONToString(data interface{}) (string, error) {
b, err := json.Marshal(data)
return string(b), err
}
// JSONFileToMap reads a JSON file from disk and returns a map containing the parsed JSON data.
// The method reads the contents of the file using the ioutil.ReadFile function, and then uses
// the json.Unmarshal function to parse the contents into a map with string keys and interface{}
// values.
//
// This method can be useful when you need to load a JSON file from disk and convert its contents
// into a map of key-value pairs dynamically, such as when you are working with configuration files
// or other JSON data stored on disk.
//
// Example usage:
//
// result, err := JSONFileToMap("path/to/file.json")
// if err != nil {
// fmt.Println("Error reading JSON file:", err)
// } else {
// fmt.Println(result)
// }
//
// Playground: https://go.dev/play/p/Tk0pqxI5E8A
func JSONFileToMap(filename string) (map[string]interface{}, error) {
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
var result map[string]interface{}
err = json.Unmarshal(data, &result)
return result, err
}
// DeepMergeJSON recursively merges two JSON objects together, combining the values of the same keys from both objects.
// The method takes two maps with string keys and interface{} values as input, and returns a new map with the merged values.
// If a key exists in both input maps, the value from the src map will overwrite the value from the dst map.
// If both values are maps, the method will recursively merge the sub-maps. Otherwise, the method will replace the
// value in the dst map with the value from the src map.
//
// This method can be useful when you need to merge two JSON objects together dynamically, such as when you are
// working with configuration files or other JSON data that needs to be merged together from multiple sources.
//
// Example usage:
//
// dst := map[string]interface{}{
// "name": "John Doe",
// "age": 30,
// "address": map[string]interface{}{
// "city": "New York",
// "country": "USA",
// },
// }
// src := map[string]interface{}{
// "age": 35,
// "address": map[string]interface{}{
// "city": "Boston",
// "state": "MA",
// },
// "phone": "123-456-7890",
// }
// result := DeepMergeJSON(dst, src)
// fmt.Println(result)
//
// Playground: https://go.dev/play/p/zAZieDE3OUf
func DeepMergeJSON(dst, src map[string]interface{}) map[string]interface{} {
for key, srcVal := range src {
if dstVal, ok := dst[key]; ok {
if srcValMap, srcValIsMap := srcVal.(map[string]interface{}); srcValIsMap {
if dstValMap, dstValIsMap := dstVal.(map[string]interface{}); dstValIsMap {
dst[key] = DeepMergeJSON(dstValMap, srcValMap)
}
} else {
dst[key] = srcVal
}
} else {
dst[key] = srcVal
}
}
return dst
}