Go struct को JSON में बदलें!

Go struct डालें और उसके json टैग के अनुसार उदाहरण JSON बनाएँ! Go (Golang) struct, json tags समर्थित। API डॉक्यूमेंटेशन और टेस्ट request body (sample JSON) के लिए उपयोगी।
Go struct
परिणाम
 

Go struct को JSON में बदलें!

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 नहीं बनाते जिसकी आपने उम्मीद की थी।

उपयोग कैसे करें

  1. type Xxx struct { ... } declarations पेस्ट करें। Grouped type ( ... ) blocks और एक साथ कई structs भी ठीक हैं।
  2. अगर कोई field दूसरा struct इस्तेमाल करता है, तो उसकी definition भी शामिल करें। इनपुट में न मौजूद बाहरी package types (जैसे decimal.Decimal) null बनते हैं।
  3. कोई options नहीं हैं: जिस पहले struct को कोई दूसरा type reference नहीं करता वही top-level JSON है, इसलिए जो चाहिए उसे सबसे पहले रखें या असंबंधित structs हटा दें।
  4. परिणाम को curl -d, Postman body या httptest-आधारित handler test में कॉपी करें।

उदाहरण

इनपुट (Go struct)
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
}
आउटपुट (JSON)
{
  "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 में लपेटे जाते हैं।
  • बिना tag वाले embedded structs parent में flatten होते हैं, नाम टकराने पर बाहरी fields जीतते हैं; tag वाला embedded struct उस नाम के तहत nested object बनता है।
  • Pointers (*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 समर्थित हैं।

उपयोग के मामले

Handler tests के लिए request bodies

जिस struct को आप c.ShouldBindJSON(&req) से bind करते हैं उसे पेस्ट करें, body बनाएं, और httptest.NewRequest या curl से endpoint call करें। Keys सीधे tags से आती हैं, इसलिए binding mismatch की संभावना कम रहती है।

Tag की गलतियां ढूंढना

मिले-जुले json:"user_id" और json:"userId" tags, या भूला हुआ tag जिससे upper-case UserID key निकल जाती है, आउटपुट में एक नज़र में दिख जाते हैं।

दूसरी भाषाओं के clients के साथ schema साझा करना

Frontend या Python consumers को अपने Go response struct का JSON दें; वे JSON → TypeScript या JSON → Python जैसे टूल्स से उसे वापस models में बदल सकते हैं।

नोट्स और सीमाएँ

  • omitempty fields sample में हमेशा दिखते हैं; असली marshaling में zero value होने पर वे हट जाते हैं।
  • nil pointer null के रूप में marshal होता है, लेकिन sample भरी हुई स्थिति दिखाता है।
  • Custom MarshalJSON() वाले types और json.RawMessage की असली सामग्री पता नहीं चल सकती, इसलिए वे प्रतिबिंबित नहीं होते।
  • Go 1.24 का omitzero और encoding/json/v2 का व्यवहार जैसे नए options शामिल नहीं हैं।

अक्सर पूछे जाने वाले प्रश्न

बिना tag वाले field को कौन सी key मिलती है?

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 structs।

JSON → Go struct पेज इस्तेमाल करें। उसका आउटपुट वापस इस पेज में डालना यह जांचने का तेज़ तरीका है कि round trip में मूल keys बनी रहती हैं।