Understanding JSON Encode and Decode in Dart for Beginners

Mobterest Studio
3 min readMar 1, 2024

--

JSON Encode and Decode in Dart

JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. In Dart, a language often used for mobile app development, encoding and decoding JSON data is a common task. Let’s dive into the concepts of encoding and decoding in the context of Dart programming.

Photo by Nate Grant on Unsplash

JSON Encode in Dart

What is JSON Encoding?

JSON encoding is the process of converting Dart objects into a JSON-formatted string. This is particularly useful when you want to send data to a server or store it in a file in a structured format. Dart provides a built-in library for encoding objects into JSON, making it a seamless process.

Example of JSON Encoding in Dart

Let’s consider a simple Dart class representing a person:

class Person {
String name;
int age;

Person(this.name, this.age);
}

Now, let’s encode an instance of this class into a JSON string:

import 'dart:convert';

void main() {
Person person = Person('John Doe', 25);
String jsonString = jsonEncode(person);
print(jsonString);
}

In this example, jsonEncode function from the dart:convert library is used to convert the Person object into a JSON-formatted string. The resulting jsonString will look like {"name":"John Doe","age":25}

JSON Decode in Dart

What is JSON Decoding?

JSON decoding is the reverse process of encoding. It involves converting a JSON-formatted string back into Dart objects. This is commonly done when retrieving data from a server or reading from a file.

Example of JSON Decoding in Dart

Let’s take the JSON string {"name":"Jane Smith","age":30} and decode it into a Dart object:

import 'dart:convert';

class Person {
String name;
int age;

Person(this.name, this.age);
}

void main() {
String jsonString = '{"name":"Jane Smith","age":30}';

Map<String, dynamic> jsonMap = jsonDecode(jsonString);

Person person = Person(jsonMap['name'], jsonMap['age']);

print('Name: ${person.name}, Age: ${person.age}');
}

In this example, jsonDecode function is used to convert the JSON string into a Map<String, dynamic> representing the JSON structure. We then create a Person object using the values from the map.

Think of encoding as preparing a package for shipping. You gather all the items (Dart objects), arrange them neatly in a box (JSON string), and put a label on it (encode). Decoding, on the other hand, is opening the package upon arrival, checking the label to understand the contents, and placing the items back where they belong.

Conclusion

Understanding JSON encoding and decoding is fundamental for mobile app developers working with Dart. It allows for smooth communication with servers and efficient data storage. Just like packing and unpacking items for shipping, encoding and decoding in Dart make it possible to send and receive data in a standardized and efficient way. To learn more about JSON Serialization in Flutter, head over here

👏🏽 👏🏽 Give this story CLAPS

👉🏽 Subscribe for upcoming articles

💰 Access Free Mobile Development tutorials

🔔 Follow for more

See you on next article 👋

--

--