JSON to Code Converter
Paste a sample of JSON and choose Go, Python or C#. Nested objects become their own types, lists of objects are merged, and fields missing from some items are marked optional.
Worked examples
Go
{"id": 1, "user_name": "ann", "orders": [{"sku": "x", "qty": 2}, {"sku": "y", "qty": 3, "note": "gift"}]}
type Root struct { ID int `json:"id"` UserName string `json:"user_name"` Orders []Order `json:"orders"` } type Order struct { Sku string `json:"sku"` Qty int `json:"qty"` Note *string `json:"note,omitempty"` }
The note field is missing from one order, so it is a pointer with omitempty.
About JSON to Code Converter
When you consume a JSON API you want typed objects, not loose maps. Writing the types by hand from a sample is repetitive: every key needs a name in the style of the language, a type, and in Go a tag that remembers the original key. A sample with nested objects and lists of objects means a dozen small types and plenty of room for a typing slip.
Paste a sample and choose Go, Python or C#. Nested objects become their own types, and every item in a list is looked at and merged, so a field that is whole in one item and fractional in another becomes a float, and a field missing from some items is marked optional: a pointer with omitempty in Go, Optional with a default of None in Python and a nullable type in C#. Python output can include from_dict methods that rebuild nested objects, and C# output can include JsonPropertyName attributes that keep the original key names.
How to use JSON to Code Converter
- Paste a JSON sample, ideally one with several list items so optional fields show up.
- Choose Go, Python or C# and name the top type.
- Set the options for the language: omitempty, from_dict methods or attributes.
- Copy the code into your project and adjust names or types if needed.
Common uses
- Writing client code for a new API
- Making types for a configuration file
- Creating test fixtures with the right shape
- Documenting the shape of a response
Frequently asked questions
How are optional fields found?
A field that is missing from at least one item in a list, or that is sometimes null, is treated as optional or nullable. Use a sample with several items for best results.
How are names converted?
Go uses exported PascalCase names and keeps common acronyms such as ID, URL and HTTP in capitals. Python uses snake_case and avoids reserved words. C# uses PascalCase, and if a property would share its type's name a suffix is added.
What types are used for numbers?
Whole numbers become int, or int64 in Go and long in C# if any value is above about 2 billion. Numbers with a decimal part become float64, float or double, and a mix of both becomes the decimal type.
What happens to a field that is always null?
A sample cannot show what it would hold, so it gets the untyped form: any in Go, Any in Python and object in C#.
What about dates?
JSON has no date type, so dates are strings in the output. Change the type to a date type in your code if you want automatic parsing.