Last active
March 22, 2019 10:58
-
-
Save dahoba/201a63cabe8b3d495d65b82602454cd9 to your computer and use it in GitHub Desktop.
Golang iterate/parse generic JSON array
This file contains hidden or 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
| // https://play.golang.org/p/ZC2RkvU5XtN | |
| package main | |
| import "fmt" | |
| import "encoding/json" | |
| func main() { | |
| payload := []byte(`[{"Key":"[email protected]:PROGRESS_UPDATE:UPD-000001", "Record":{"contractId":"UPD-000001","type":3,"status":0,"createDate":"2019-03-15T12:07:48+07:00","content":"mock content","groupOfSignatures":[{"participantId":"[email protected]","signature":"alice's signature"},{"participantId":"[email protected]","signature":""}],"message":"progress update for Inv 01","generalReferences":["n/a"],"pendingBy":"","referenceToContractId":""}},{"Key":"[email protected]:PROGRESS_UPDATE:UPD-000010", "Record":{"contractId":"UPD-000010","type":3,"status":4,"createDate":"2019-03-15T12:07:48+07:00","content":"mock content","groupOfSignatures":[{"participantId":"[email protected]","signature":"alice's signature"},{"participantId":"[email protected]","signature":""}],"message":"progress update for Inv 01","generalReferences":["n/a"],"pendingBy":"","referenceToContractId":""}},{"Key":"[email protected]:PROGRESS_UPDATE:UPD-000020", "Record":{"contractId":"UPD-000020","type":3,"status":2,"createDate":"2019-03-15T12:07:48+07:00","content":"mock content","groupOfSignatures":[{"participantId":"[email protected]","signature":"alice's signature"},{"participantId":"[email protected]","signature":"bob's signature"}],"message":"progress update for Inv 01","generalReferences":["n/a"],"pendingBy":"","referenceToContractId":""}},{"Key":"[email protected]:TRADE:INV-000001", "Record":{"contractId":"INV-000001","type":0,"status":0,"createDate":"2019-03-15T12:07:48+07:00","content":"IPFS Invoice 01","groupOfSignatures":[{"participantId":"[email protected]","signature":"alice's signature"},{"participantId":"[email protected]","signature":""}],"message":"Invoice 01","generalReferences":["n/a"],"pendingBy":"","progressReferences":null,"cancelReferences":null,"sideLetterReferences":null}},{"Key":"[email protected]:TRADE:INV-000080", "Record":{"contractId":"INV-000080","type":0,"status":2,"createDate":"2019-03-15T12:07:48+07:00","content":"IPFS Invoice 01","groupOfSignatures":[{"participantId":"[email protected]","signature":"alice's signature"},{"participantId":"[email protected]","signature":"bob's signature"}],"message":"Invoice 01","generalReferences":["n/a"],"pendingBy":"","progressReferences":null,"cancelReferences":null,"sideLetterReferences":null}},{"Key":"[email protected]:TRADE:INV-000099", "Record":{"contractId":"INV-000099","type":0,"status":3,"createDate":"2019-03-15T12:07:48+07:00","content":"IPFS Invoice 01","groupOfSignatures":[{"participantId":"[email protected]","signature":"alice's signature"},{"participantId":"[email protected]","signature":""}],"message":"Invoice 01","generalReferences":["n/a"],"pendingBy":"","progressReferences":null,"cancelReferences":null,"sideLetterReferences":null}}]`) | |
| var v interface{} | |
| json.Unmarshal(payload, &v) | |
| data := v.([]interface{}) | |
| for k, v := range data { | |
| switch v := v.(type) { | |
| case string: | |
| fmt.Println(k, v, "(string)") | |
| case float64: | |
| fmt.Println(k, v, "(float64)") | |
| case []interface{}: | |
| fmt.Println(k, "(array):") | |
| for i, u := range v { | |
| fmt.Println(" ", i, u) | |
| } | |
| case map[string]interface{}: | |
| fmt.Println(k, "(map array):") | |
| for i, u := range v { | |
| fmt.Println(" ", i, u) | |
| } | |
| default: | |
| fmt.Println(k, v, "(unknown)") | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment