JSON को encoding/json tags वाली Go struct definitions में बदलें। Field नाम Go के exported-name नियम और golint की initialism सूची (ID, URL, API, ...) का पालन करते हैं, और columns ऐसे aligned होते हैं जैसे कोड gofmt से गुज़रा हो।
यह तब काम आता है जब webhook payload या API response को json.Unmarshal करने के लिए type चाहिए, net/http, Gin या Echo handlers के लिए request-body struct चाहिए, या किसी CLI के लिए config struct।
omitempty tag मिलता है।Struct का नाम में top-level struct का नाम डालें (default Root); यह URL में ?name= के रूप में रहता है।.go फ़ाइल में पेस्ट करें और package clause जोड़ें। अगर time fields मिले हैं, तो import "time" शामिल होता है।var ev WebhookEvent; err := json.Unmarshal(body, &ev) के रूप में इस्तेमाल करें।{
"event_id": "evt_1Pq",
"type": "invoice.paid",
"created_at": "2024-05-01T03:15:00Z",
"amount_cents": 4999,
"customer": { "id": "cus_9", "email": "kim@example.com" },
"retry_count": null
}import "time"
type WebhookEvent struct {
EventID string `json:"event_id"`
Type string `json:"type"`
CreatedAt time.Time `json:"created_at"`
AmountCents int `json:"amount_cents"`
Customer Customer `json:"customer"`
RetryCount any `json:"retry_count,omitempty"`
}
type Customer struct {
ID string `json:"id"`
Email string `json:"email"`
}
golint के initialism नियम से event_id EventID बनता है, और RFC 3339 string created_at को time.Time माना जाता है, जिससे import "time" जुड़ता है। retry_count null है, इसलिए उसका type अज्ञात है: वह omitempty के साथ any बनता है।
json:"originalKey" tag, और कुछ rows में न मौजूद या null वाले fields के लिए ,omitempty।int, string, bool, structs, time.Time) pointers (*int) बनते हैं, ताकि "मौजूद नहीं" और zero value में फ़र्क रहे। Slices और any पहले से nil हो सकते हैं, इसलिए pointer नहीं बनते।int, उससे बड़े के लिए int64, decimals आने पर float64।2024-05-01T03:15:00Z जैसी RFC 3339 strings को time.Time माना जाता है।user_id → UserID, home_url → HomeURL); अंक से शुरू होने वाली keys को Field prefix मिलता है।"", वे छोड़ दी जाती हैं और struct के ऊपर comment में सूचीबद्ध होती हैं।[]Root में unmarshal करने का comment।Payment या Git hosting service के भेजे sample event से struct बनाएं और map[string]any पर बार-बार type assertions की बजाय उसे json.NewDecoder(r.Body).Decode(&ev) से decode करें।
Client request sample से c.ShouldBindJSON(&req) के लिए struct बनाएं, फिर जहां ज़रूरी हो binding:"required" tags जोड़ें।
config.json को एक बार struct में बदलें, और os.ReadFile के बाद एक ही json.Unmarshal से पूरी config typed values के रूप में मिल जाती है।
time.Time सिर्फ़ RFC 3339 strings के लिए अनुमानित होता है; 2024-05-01 03:15:00 जैसी values या Unix timestamps string या int रहते हैं।json.Unmarshal default रूप से अज्ञात keys को अनदेखा करता है और keys को case-insensitive तरीके से match करता है। सख्त decoding चाहिए तो Decoder.DisallowUnknownFields() इस्तेमाल करें।int 64-bit होता है, लेकिन int64 तभी लिखा जाता है जब कोई sample वैल्यू 32 bits से बड़ी हो। बढ़ सकने वाली IDs के लिए खुद int64 पर बदलना ज़्यादा सुरक्षित है।null रहने वाली values any बनती हैं; असली type पता चलने पर उन्हें बदलें।*int जैसे pointers क्यों हैं?सादा int field key न होने पर 0 होता है, जो असली 0 से अलग नहीं पहचाना जा सकता। Pointer key न होने या null होने पर nil होता है, और omitempty के साथ दोबारा marshal करने पर key छोड़ दी जाती है।
encoding/json सिर्फ़ exported (कैपिटल) fields पढ़ता और लिखता है; lower-case fields tag होने पर भी अनदेखे होते हैं। Go में नाम कैपिटल रहते हैं और JSON key tag में जाती है।
retry_count जैसा null field any के रूप में आया।Sample में सिर्फ़ null होने पर type अनुमानित करने के लिए कुछ नहीं होता। असली वैल्यू वाला दूसरा response array में जोड़कर दोनों बदलें, या हाथ से *int जैसा कुछ कर दें।
हां। ऊपर के Go struct → JSON टैब में struct पेस्ट करें; वह tags पढ़कर उदाहरण JSON बनाता है।