JSON is the most common way to send data between servers and Flutter apps. Dart provides built-in tools to convert JSON into Dart objects and back to JSON again. This guide explains how to convert JSON to Dart models and how to use them in real Flutter projects.
JSON to Dart conversion lets you map JSON data into Dart model classes. This gives you:
{
"id": 5,
"name": "John Doe",
"email": "john@example.com",
"isActive": true
}Use dart:convert to decode JSON.
import 'dart:convert';
void main() {
const jsonString = '{"id":1,"name":"Alice"}';
final data = jsonDecode(jsonString);
print(data['name']); // Alice
}class User {
final int id;
final String name;
final String email;
User({
required this.id,
required this.name,
required this.email,
});
factory User.fromJson(Map<String, dynamic> json) {
return User(
id: json['id'],
name: json['name'],
email: json['email'],
);
}
Map<String, dynamic> toJson() {
return {
"id": id,
"name": name,
"email": email,
};
}
}final jsonString = '[{"id":1,"name":"A"},{"id":2,"name":"B"}]';
final List<dynamic> decoded = jsonDecode(jsonString);
final users = decoded.map((u) => User.fromJson(u)).toList();import 'dart:convert';
import 'dart:io';
void main() async {
final file = File('assets/user.json');
final contents = await file.readAsString();
final data = jsonDecode(contents);
print(data);
}In Flutter, put files inside assets/and add them in pubspec.yaml.
No. Dart already supports JSON parsing using dart:convert.
Create nested model classes and call childModel.fromJson().
Use nullable types like String?.
Powerful JSON tools and easy-to-understand guides for developers.
Format & pretty-print JSON instantly.
Open Tool →ViewerExplore JSON in a tree view.
Open Tool →FormatterBeautify JSON with indentation.
Open Tool →ValidatorValidate JSON & detect errors.
Open Tool →UtilityCompare two JSON files.
Open Tool →ConverterGenerate Dart model classes.
Open Tool →ConverterConvert JSON to CSV format.
Open Tool →ConverterExport JSON to Excel.
Open Tool →EditorEdit JSON online.
Open Tool →