Json

Transposing JSON Object using Google Apps Script

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.

var keys = ["key1", "key2", "key3", "key4", "key5"];
var result = {};
data.map(function(_, i){return keys.map(function(f, j){return data[i][keys[j]]})}).forEach(function(e, i){result[keys[i]] = e});
Logger.log(JSON.stringify(result))

Copying Values from JSON to Struct using reflect Package

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.ValueOf(obj).Elem()
	typeOfT := s.Type()
	for i := 0; i < s.NumField(); i++ {
		for j, f := range d {
			if typeOfT.Field(i).Tag.Get("json") == j {
				fl := s.FieldByName(typeOfT.Field(i).Name)
				switch fl.Kind() {
				case reflect.Bool:
					fl.SetBool(f.(bool))
				case reflect.Int, reflect.Int64:
					c, _ := f.(float64)
					fl.SetInt(int64(c))
				case reflect.String:
					fl.SetString(f.(string))
				}
			}
		}
	}
	fmt.Printf("%+v\n", obj) // &{Key1:v1 Key2:v2 Key3:1234567890 Key4:456 Key5:true}
}

Result :

&{Key1:v1 Key2:v2 Key3:1234567890 Key4:456 Key5:true}

The Go Playground

https://play.golang.org/p/Rz-GNbFyDfh

Parsing JSON object (keys are number and changing every time)

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.Unmarshal([]byte(data), &k1)
    for i, e := range k1.Key1.Key2 {
        if key, ok := e.(map[string]interface{})[strconv.Itoa(i)].([]interface{}); ok {
            for _, f := range key {
                fmt.Printf("%+v\n", f.(map[string]interface{})["key3"])
            }
        }
    }
}

Result:

value3a
value3b
value3c
value3d

The Go Playground

https://play.golang.org/p/xm2KvgOIkKH