Go struct definitions पेस्ट करें और marshaling के समय encoding/json जो नियम लागू करता है, उन्हीं से बना उदाहरण JSON document पाएं: json:"..." tag नाम, - से छिपाए fields, ,string option, unexported lowercase fields और flatten किए गए embedded structs।
यह Gin, Echo या net/http request struct से curl body बनाने, gRPC-Gateway या REST docs के लिए samples तैयार करने, और उन fields को पकड़ने में काम आता है जिनके tags वह key नहीं बनाते जिसकी आपने उम्मीद की थी।
type Xxx struct { ... } declarations पेस्ट करें। Grouped type ( ... ) blocks और एक साथ कई structs भी ठीक हैं।decimal.Decimal) null बनते हैं।curl -d, Postman body या httptest-आधारित handler test में कॉपी करें।type Base struct {
ID string `json:"id"`
CreatedAt time.Time `json:"created_at"`
}
type Job struct {
Base
Queue string `json:"queue"`
Attempts int `json:"attempts,string"`
Labels map[string]string `json:"labels,omitempty"`
TimeoutSec *int `json:"timeout_sec,omitempty"`
Token string `json:"-"`
Done bool
lastErr error
}{
"id": "",
"created_at": "0001-01-01T00:00:00Z",
"queue": "",
"attempts": "0",
"labels": {
"key": ""
},
"timeout_sec": 0,
"Done": false
}Embedded Base id और created_at को सीधे Job में जोड़ता है, और ,string tag वाला Attempts number की जगह string "0" बनता है। Token (tag -) और unexported lastErr हटा दिए जाते हैं, जबकि बिना tag वाला Done अपना Go field नाम key के रूप में रखता है। time.Time Go की zero value 0001-01-01T00:00:00Z दिखाता है।
json:"name" tag को key के रूप में इस्तेमाल करता है, और tag न होने पर ठीक field नाम पर लौटता है।json:"-" fields और unexported lowercase fields छोड़ देता है; json:"-," field को शाब्दिक key - के तहत रखता है।,string के साथ numeric और boolean fields "0" और "false" जैसी strings में लपेटे जाते हैं।*int, *Profile) pointed-to type से भरे जाते हैं, और type Status string जैसे defined types अपने underlying type का पालन करते हैं।[]byte Base64 (खाली) string बनता है, map[int]T में "0" key होती है, time.Duration nanosecond integer 0 है, और interface{}/any null हैं।Children []Node जैसे self-referencing structs खाली array पर रुक जाते हैं। Anonymous nested struct fields समर्थित हैं।जिस struct को आप c.ShouldBindJSON(&req) से bind करते हैं उसे पेस्ट करें, body बनाएं, और httptest.NewRequest या curl से endpoint call करें। Keys सीधे tags से आती हैं, इसलिए binding mismatch की संभावना कम रहती है।
मिले-जुले json:"user_id" और json:"userId" tags, या भूला हुआ tag जिससे upper-case UserID key निकल जाती है, आउटपुट में एक नज़र में दिख जाते हैं।
Frontend या Python consumers को अपने Go response struct का JSON दें; वे JSON → TypeScript या JSON → Python जैसे टूल्स से उसे वापस models में बदल सकते हैं।
omitempty fields sample में हमेशा दिखते हैं; असली marshaling में zero value होने पर वे हट जाते हैं।nil pointer null के रूप में marshal होता है, लेकिन sample भरी हुई स्थिति दिखाता है।MarshalJSON() वाले types और json.RawMessage की असली सामग्री पता नहीं चल सकती, इसलिए वे प्रतिबिंबित नहीं होते।omitzero और encoding/json/v2 का व्यवहार जैसे नए options शामिल नहीं हैं।encoding/json exported field नाम को शब्दशः इस्तेमाल करता है, इसलिए Done bool "Done" बनता है। Go में decoding case-insensitive रूप से match करती है, लेकिन दूसरी भाषाओं के clients ऐसा नहीं करते, इसलिए explicit tags ज़्यादा सुरक्षित हैं।
time.Time वर्ष 0001 पर क्यों सेट है?Go का zero time.Time 0001-01-01 00:00:00 UTC है, जो RFC 3339 में 0001-01-01T00:00:00Z के रूप में marshal होता है। इसीलिए यह दूसरे → JSON पेजों की 1970 वाली तारीखों से अलग है।
json:",string" कब उपयोगी है?ज़्यादातर बड़े int64 IDs के लिए जिन्हें JavaScript ठीक-ठीक नहीं दर्शा सकता, इसलिए वे strings के रूप में भेजे जाते हैं। Sample "0" दिखाता है, जिससे clients को साफ़ पता चलता है कि उन्हें string मिलेगी।
JSON → Go struct पेज इस्तेमाल करें। उसका आउटपुट वापस इस पेज में डालना यह जांचने का तेज़ तरीका है कि round trip में मूल keys बनी रहती हैं।