Saturday, January 18, 2025
HomeProgrammingWhat Is Deserialize and Serialize in JSON?

What Is Deserialize and Serialize in JSON?

Serialization and deserialization allow data to be converted between different formats for storage, transmission, and processing.

Serialization

Serialization is converting an object, data structure, or other data type into a format that can be easily stored or transmitted. In the case of JSON, serialization involves converting data into a JSON string.

Example in Python

import json

# Python dictionary
data = {"name": "John", "age": 30, "city": "New York"}

# Serialize the dictionary to a JSON string
json_string = json.dumps(data)
print(json_string)

Output:

{"name": "John", "age": 30, "city": "New York"}

Deserialization

Deserialization is the reverse process of serialization, where a JSON string is converted back into an object, such as a dictionary in Python, an object in JavaScript, or a class instance in Java.

See also  How to unpack pkl file - python

Example in Python

import json

# JSON string
json_string = '{"name": "John", "age": 30, "city": "New York"}'

# Deserialize the JSON string to a Python dictionary
data = json.loads(json_string)
print(data)

Output:

{'name': 'John', 'age': 30, 'city': 'New York'}

Use Cases of Serialization and Deserialization

  1. Data Storage: Saving objects or data to files or databases in JSON format.
  2. Data Transmission: Sending data over a network (e.g., APIs) in JSON format.
  3. Configuration: Storing and reading configuration files in JSON.
  4. Data Interchange: Sharing data between different systems or programming languages.
See also  What is the Equivalent of the ls() Function of R in Python

Serialization and Deserialization in Other Languages

JavaScript

Serialization:

const data = { name: "John", age: 30, city: "New York" };
const jsonString = JSON.stringify(data);
console.log(jsonString);

Deserialization:

const jsonString = '{"name": "John", "age": 30, "city": "New York"}';
const data = JSON.parse(jsonString);
console.log(data);

Java

Serialization:

import com.google.gson.Gson;

public class Main {
    public static void main(String[] args) {
        Gson gson = new Gson();
        Person person = new Person("John", 30, "New York");
        String jsonString = gson.toJson(person);
        System.out.println(jsonString);
    }
}

class Person {
    String name;
    int age;
    String city;
    
    Person(String name, int age, String city) {
        this.name = name;
        this.age = age;
        this.city = city;
    }
}

Deserialization:

import com.google.gson.Gson;

public class Main {
    public static void main(String[] args) {
        String jsonString = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}";
        Gson gson = new Gson();
        Person person = gson.fromJson(jsonString, Person.class);
        System.out.println(person.name);
    }
}

Benefits of JSON Serialization

  1. Lightweight: JSON is easy to read and has a smaller size compared to XML.
  2. Cross-Platform: JSON is supported across many programming languages.
  3. Human-Readable: JSON data is easy to understand.
RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x