Dart JSON serialization quick reference
The three patterns you need for every Flutter project.
Object → JSON string
Implement Map<String, dynamic> toJson() on your class, then call jsonEncode(obj.toJson()) from dart:convert. The result is a String you can send as an HTTP body.
JSON string → Object
Call jsonDecode(jsonString) to get a Map<String, dynamic>, then pass it to your MyClass.fromJson(map) factory constructor. Use the JSON to Dart tool to generate fromJson.
With dio HTTP client
Pass model.toJson() directly as the data parameter in dio — dio.post('/endpoint', data: model.toJson()). Dio serializes Map<String, dynamic> automatically, no jsonEncode() needed.
With Firestore
Write: doc.set(model.toJson()). Read: MyClass.fromJson(snap.data()!). The toJson() map maps directly to Firestore document fields — no extra conversion layer needed.
Related tools
Popular searches
dart toJson method
dart object to json string
flutter model to json
dart jsonEncode object
dart serialize object to json
flutter json serialize
dart to json converter
dart toMap method
dart convert object to map
dart json encode model
Dart JSON serialization — the most common questions.
How do I add a toJson method to a Dart class?
A toJson() method returns Map<String, dynamic> with each field mapped to its JSON key. For nested objects call their own toJson(). For lists of objects use .map((e) => e.toJson()).toList(). Paste your class above and JSONshift generates the complete implementation.
How do I convert a Dart object to a JSON string?
Two steps: implement toJson() returning Map<String, dynamic>, then call jsonEncode(obj.toJson()) from dart:convert. The result is a String you can use as an HTTP body, write to SharedPreferences, or log for debugging.
What is the difference between toJson() and toMap() in Dart?
They are functionally identical — both return Map<String, dynamic>. toJson() is the more common naming convention and aligns with json_serializable. toMap() is sometimes used when the map isn't specifically for JSON serialization, e.g. for Firestore or SQLite. Use whichever matches your team's convention.
How do I handle nullable fields in toJson with null safety?
Simply include the nullable field in the map — it will serialize as null in the JSON. For example: 'email': email. If you want to omit null fields from the JSON output entirely, use: if (email != null) 'email': email inside the map using a spread with a conditional, or filter the map after building it.