Gists
This is a sample script for transposing JSON object using Google Apps Script.
Input data : [ {"key1":"a1","key2":"a2","key3":"a3","key4":"a4","key5":"a5"}, {"key1":"b1","key2":"b2","key3":"b3","key4":"b4","key5":"b5"}, {"key1":"c1","key2":"c2","key3":"c3","key4":"c4","key5":"c5"}, {"key1":"d1","key2":"d2","key3":"d3","key4":"d4","key5":"d5"}, {"key1":"e1","key2":"e2","key3":"e3","key4":"e4","key5":"e5"} ] Output data : { "key1": ["a1", "b1", "c1", "d1", "e1"], "key2": ["a2", "b2", "c2", "d2", "e2"], "key3": ["a3", "b3", "c3", "d3", "e3"], "key4": ["a4", "b4", "c4", "d4", "e4"], "key5": ["a5", "b5", "c5", "d5", "e5"] } Script : At first, keys have to be defined by yourself, because the order of json is not decided.
Gists
This is a sample script for copying values from JSON to a struct using reflect package.
Script : package main
import (
"encoding/json"
"fmt"
"reflect"
)
type obj struct {
Key1 string `json:"k1"`
Key2 string `json:"k2"`
Key3 int64 `json:"k3"`
Key4 int `json:"k4"`
Key5 bool `json:"k5"`
}
func main() {
data := `{"k1": "v1", "k2": "v2", "k3": 1234567890, "k4": 456, "k5": true}`
d := map[string]interface{}{}
json.Unmarshal([]byte(data), &d)
obj := &obj{}
s := reflect.
Gists
This sample script is for parsing JSON object. In the object, the keys are number and changing every time.
Object: { "key1": { "key2": [ {"0": [{"key3": "value3a"}, {"key3": "value3b"}]}, {"1": [{"key3": "value3c"}, {"key3": "value3d"}]} ] } } Script: package main import ( "encoding/json" "fmt" "strconv" ) type key1 struct { Key1 key2 `json:"key1"` } type key2 struct { Key2 []interface{} `json:"key2"` } func main() { data := `{"key1": {"key2": [{"0": [{"key3": "value3a"}, {"key3": "value3b"}]},{"1": [{"key3": "value3c"}, {"key3": "value3d"}]}]}}` k1 := &key1{} json.