JSON to Objective-C
Paste a JSON sample. Get Objective-C types, and the code that reads and writes the JSON. It runs in your browser, and your data never leaves it.
Open the tool with Objective-C chosen
An example
This JSON:
{
"id": 42,
"name": "Ada Lovelace",
"active": true,
"joined": "2020-01-02T03:04:05Z",
"tags": ["engineer", "writer"],
"address": { "city": "London", "postcode": null },
"scores": [9.5, 8, 10]
}
becomes this Objective-C:
// QTRoot.h
#import <Foundation/Foundation.h>
@class QTRoot;
@class QTAddress;
NS_ASSUME_NONNULL_BEGIN
#pragma mark - Object interfaces
@interface QTRoot : NSObject
@property (nonatomic, assign) NSInteger identifier;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) BOOL isActive;
@property (nonatomic, copy) NSString *joined;
@property (nonatomic, copy) NSArray<NSString *> *tags;
@property (nonatomic, strong) QTAddress *address;
@property (nonatomic, copy) NSArray<NSNumber *> *scores;
@end
@interface QTAddress : NSObject
@property (nonatomic, copy) NSString *city;
@property (nonatomic, nullable, copy) id postcode;
@end
NS_ASSUME_NONNULL_END
// QTRoot.m
#import "QTRoot.h"
@implementation QTRoot
@end
@implementation QTAddress
@end
With the reader and the writer
The tool also writes the code that turns the text into those types, and back again. That code checks the shape as it reads, which is the part a type alone cannot do.
// QTRoot.h
// To parse this JSON:
//
// NSError *error;
// QTRoot *root = [QTRoot fromJSON:json encoding:NSUTF8Encoding error:&error];
#import <Foundation/Foundation.h>
@class QTRoot;
@class QTAddress;
NS_ASSUME_NONNULL_BEGIN
#pragma mark - Object interfaces
@interface QTRoot : NSObject
@property (nonatomic, assign) NSInteger identifier;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) BOOL isActive;
@property (nonatomic, copy) NSString *joined;
@property (nonatomic, copy) NSArray<NSString *> *tags;
@property (nonatomic, strong) QTAddress *address;
@property (nonatomic, copy) NSArray<NSNumber *> *scores;
+ (_Nullable instancetype)fromJSON:(NSString *)json encoding:(NSStringEncoding)encoding error:(NSError *_Nullable *)error;
+ (_Nullable instancetype)fromData:(NSData *)data error:(NSError *_Nullable *)error;
- (NSString *_Nullable)toJSON:(NSStringEncoding)encoding error:(NSError *_Nullable *)error;
- (NSData *_Nullable)toData:(NSError *_Nullable *)error;
@end
@interface QTAddress : NSObject
@property (nonatomic, copy) NSString *city;
@property (nonatomic, nullable, copy) id postcode;
@end
NS_ASSUME_NONNULL_END
// QTRoot.m
#import "QTRoot.h"
#define λ(decl, expr) (^(decl) { return (expr); })
static id NSNullify(id _Nullable x) {
and 188 more lines. Open the tool to see the whole file.
What you need in your project
Nothing. `NSJSONSerialization` is in the standard library.
The output is a header and an implementation for each type, in the style a Cocoa project expects.
The trap to know about
A null in JSON becomes `NSNull`, not `nil`. The reader handles it; your own code must not assume `nil`.
The options you can change
These belong to Objective-C, so the row of switches changes with the language.
| Option | What it does | Default |
|---|---|---|
| Features | Interface and implementation | all | interface | implementation |
| Types only | Plain types only | false |
| C-style functions | C-style functions | false |
| Class prefix | Class prefix | QT |
| Extra comments | Extra comments | false |
Questions
How do I turn JSON into Objective-C?
Paste your JSON into the box, choose Objective-C, and the code appears at once. Give the top type a name, and every other name follows from your keys. Then copy the code, or download it as a file.
Does my JSON go to a server?
No. The generator runs inside your browser, in a worker. Nothing is uploaded, nothing is stored, and the tool works with the network turned off.
What do I need to add to my Objective-C project?
Nothing. `NSJSONSerialization` is in the standard library.
How does it know which fields are optional?
From your sample. A key that is missing, or holds null, becomes optional. One sample cannot show everything, so give more than one, or start from a JSON Schema.
Other languages
Try it on your own JSON. The generator runs in your browser. Nothing is uploaded, and nothing is stored on a server.
Open JSON to Code