JSON to Objective-C Class Generator
CV-76Paste a JSON sample and press Convert to get model classes with initWithDictionary:. It infers the types from every item in your sample, and it runs in your browser.
Runs in your browser. Nothing is uploaded.
JSON
Objective-C
How to use it
- Paste a JSON sample into the input box, or open a .json file. Use a sample that shows every key you expect.
- Pick a class prefix such as XX, or none.
- Press Convert, then copy the result or download it as a file.
- Parse with NSJSONSerialization, then call [[Root alloc] initWithDictionary:dictionary].
How the classes are built
Each object becomes an NSObject subclass with properties, an initWithDictionary: initializer and a dictionary method that writes it back. The output has a header part and an implementation part, split by a comment line, so copy each into its own .h or .m file. Strings are NSString, arrays are NSArray with lightweight generics, nested objects are their own classes, and numbers and booleans use NSNumber when a key is null or missing in some items. Whole numbers use NSInteger, or long long beyond 32 bits, decimals use double and booleans use BOOL.
How the JSON is read
If the document is a top-level array, its items are merged into one type named after the root name with Item added. The JSON is read with a parser that keeps number text exactly, so a big integer is not rounded before its type is chosen. Every object in the sample is merged by key, so a key missing from some objects is marked optional and a null in some items marks the key nullable. Keys that are not valid Objective-C names, such as first-name, are renamed, and the original key is kept in the dictionary key used in initWithDictionary: and dictionary.
What a sample cannot tell it
The result only reflects the sample you paste: a key that never appears has no member, and a number that is always whole in the sample is typed as whole even if other data holds decimals. Dates stay NSString; convert them with NSDateFormatter if you need NSDate values. Check the result against the API documentation before you rely on it.
Questions people ask
How do I convert JSON to an Objective-C class?
Paste the JSON into the input box, choose your options and press Convert to get the classes. Parse with NSJSONSerialization, then call [[Root alloc] initWithDictionary:dictionary]. The conversion happens in your browser.
Where do the header and implementation go in the Objective-C output?
The output holds both, split by two comment lines. Copy the part under the header comment into a .h file and the part under the implementation comment into a .m file, then import the header wherever you use the classes.
How do I load JSON into the generated Objective-C classes?
Parse the data with NSJSONSerialization into an NSDictionary, then call initWithDictionary: on the first class. Each value is checked with isKindOfClass: first, so a value of the wrong type is skipped and leaves its property empty.
Why is an Objective-C property named idValue or theNewName?
Some names clash with C words, NSObject methods or ARC naming rules. Keys such as id, class and description get a Value suffix, and names starting with new, copy, init or alloc get a the prefix. The original key is still used when reading and writing.
What happens when my JSON starts with an array?
The tool reads every item and merges them into one type, so a key present in only some items becomes optional. The output names that type after the root name with Item added, and a comment at the top says the document is a list. The Objective-C classes are written from the parsed sample.
How are very large numbers in the JSON sample handled?
Number text is kept exactly while types are picked, so a value like 12345678901 gets a 64-bit type where the language has one, and an integer beyond 64 bits is flagged rather than silently rounded. JSON Schema only records integer or number. The Objective-C classes are written from the parsed sample.
About this tool
The classes are inferred from the JSON you paste, so they only know what the sample shows. Every object becomes an NSObject subclass with properties, an initWithDictionary: initializer that checks each value with isKindOfClass: before using it, and a dictionary method that writes the object back. The output holds a header section and an implementation section, separated by a comment line. Strings are NSString, arrays are NSArray with lightweight generics, nested objects are their own classes, and numbers use NSInteger, long long, double or BOOL, with NSNumber for keys that are null or missing in some items. Names that clash with C, Objective-C or ARC naming rules get a suffix or prefix, for example id becomes idValue. Values with the wrong type, and list items that fail to convert, are skipped rather than raising an error. Everything runs in your browser; nothing is uploaded.