JSONExport: JSON to Model
JSONExport: JSON to Model
We look at how the open source JSONExport npm package can make creating models from JSON data much easier. Read on to get started!
Join the DZone community and get the full member experience.Join For Free
Code something amazing with the IBM library of open source blockchain patterns. Content provided by IBM.
With JSONExport, you do not need to understand your JSON structure and create your model manually. Here is a utility to convert your JSON into model to do that. You just need to paste your JSON into the JSON field. First, it will check that JSON is valid and then it will convert it into a model. It will create different files for each class.
How to Use JSONExport
To use this tool, you first need to install it from here.
If you receive any alert like given below then open your System Preferences > Security & Privacy.
Now click on Open Anyway. It will open this app in your system.
If you want to customize it you can download the source code from GitHub. Open this in Xcode and run the application.
Now, open this app and enter your JSON or import the .json file here:
It will convert your JSON into models according to the selected language. It will give you the given options for the language.
These are the supported languages:
- Java for Android
- Java for Realm Android
- Java GSON for Android
- Swift Classes
- Swift Classes for SwiftyJSON library
- Swift Classes for Realm
- Swift - CoreData
- Swift Structures
- Swift Structures for Gloss
- Swift Mappable Classes for (Swift 3) ObjectMapper
- Swift Structures for Unbox
- Objective-C - iOS.
- Objective-C - MAC.
- Objective-C - CoreData.
- Objective-C for Realm iOS.
Here you can see your model in a second window. It will give your class as a property name and the first class will be named
BaseClass. Here are some more options for you at the bottom.
Root class name: To change base class name Enter your Class Name in Root class name field.
Classes Prefix: If you want to add some prefix before the class name, enter it here.
Parent class name: If you want to inherit your all model classes with a single class, enter the name of class in that field.
There are more options:
- If you want to create a constructor in each model class, select the Constructors check box.
- If you want to add Utility methods in the model classes, then select the Utility Methods check box.
Now your model is ready to use. Use just have to put it in the right place. There is a "Save" button at bottom right corner. Save your file in your project.
Save your time and reduce efforts by using JSONExport.
Published at DZone with permission of Chanchal Chauhan . See the original article here.
Opinions expressed by DZone contributors are their own.