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

JSON डालें और उसे json टैग वाले Go struct में बदलें! Go (Golang) struct, json tags समर्थित।
JSON
परिणाम

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

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।

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

  1. JSON पेस्ट करें। कई rows वाले array में, कुछ rows में न मौजूद fields पहचाने जाते हैं और उन्हें omitempty tag मिलता है।
  2. Struct का नाम में top-level struct का नाम डालें (default Root); यह URL में ?name= के रूप में रहता है।
  3. परिणाम को .go फ़ाइल में पेस्ट करें और package clause जोड़ें। अगर time fields मिले हैं, तो import "time" शामिल होता है।
  4. इसे var ev WebhookEvent; err := json.Unmarshal(body, &ev) के रूप में इस्तेमाल करें।

उदाहरण

इनपुट (JSON)
{
  "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
}
आउटपुट (Go)
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 बनता है।

समर्थित सुविधाएँ

  • हर field पर json:"originalKey" tag, और कुछ rows में न मौजूद या null वाले fields के लिए ,omitempty।
  • Optional value types (int, string, bool, structs, time.Time) pointers (*int) बनते हैं, ताकि "मौजूद नहीं" और zero value में फ़र्क रहे। Slices और any पहले से nil हो सकते हैं, इसलिए pointer नहीं बनते।
  • संख्याएं: 32-bit integers के लिए int, उससे बड़े के लिए int64, decimals आने पर float64।
  • 2024-05-01T03:15:00Z जैसी RFC 3339 strings को time.Time माना जाता है।
  • आम initialisms upper-case रहते हैं (user_id → UserID, home_url → HomeURL); अंक से शुरू होने वाली keys को Field prefix मिलता है।
  • जो keys Go json tag में नहीं आ सकतीं, जैसे "", वे छोड़ दी जाती हैं और struct के ऊपर comment में सूचीबद्ध होती हैं।
  • Objects का top-level array element के लिए struct बनता है, साथ में []Root में unmarshal करने का comment।

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

Webhook receivers

Payment या Git hosting service के भेजे sample event से struct बनाएं और map[string]any पर बार-बार type assertions की बजाय उसे json.NewDecoder(r.Body).Decode(&ev) से decode करें।

Gin या Echo में request binding

Client request sample से c.ShouldBindJSON(&req) के लिए struct बनाएं, फिर जहां ज़रूरी हो binding:"required" tags जोड़ें।

JSON config पढ़ने वाले CLIs

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() इस्तेमाल करें।
  • 64-bit platforms पर int 64-bit होता है, लेकिन int64 तभी लिखा जाता है जब कोई sample वैल्यू 32 bits से बड़ी हो। बढ़ सकने वाली IDs के लिए खुद int64 पर बदलना ज़्यादा सुरक्षित है।
  • Nested structs सभी अलग top-level types के रूप में define होते हैं; कोई anonymous inline struct इस्तेमाल नहीं होता।
  • मिले-जुले type की values और हमेशा null रहने वाली values any बनती हैं; असली type पता चलने पर उन्हें बदलें।

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

Optional fields *int जैसे pointers क्यों हैं?

सादा int field key न होने पर 0 होता है, जो असली 0 से अलग नहीं पहचाना जा सकता। Pointer key न होने या null होने पर nil होता है, और omitempty के साथ दोबारा marshal करने पर key छोड़ दी जाती है।

क्या field नाम lower-case कर सकते हैं?

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 को वापस sample JSON में बदल सकते हैं?

हां। ऊपर के Go struct → JSON टैब में struct पेस्ट करें; वह tags पढ़कर उदाहरण JSON बनाता है।