How to: Serialize and Deserialize JSON Data

JSON (JavaScript Object Notation) is an efficient data encoding format that enables fast exchanges of small amounts of data between client browsers and AJAX-enabled Web services.

This topic demonstrates how to serialize .NET type objects into JSON-encoded data and then deserialize data in the JSON format back into instances of .NET types using the DataContractJsonSerializer. This example uses a data contract to demonstrate serialization and deserialization of a user-defined Person type.

Normally, JSON serialization and deserialization is handled automatically by Windows Communication Foundation (WCF) when you use data contract types in service operations that are exposed over AJAX-enabled endpoints. However, in some cases you may need to work with JSON data directly - this is the scenario that this topic demonstrates.


If an error occurs during serialization of an outgoing reply on the server or the reply operation throws an exception for some other reason, it may not get returned to the client as a fault.

This topic is based on the JSON Serialization sample.

To define the data contract for a Person

  1. Define the data contract for Person by attaching the DataContractAttribute to the class and DataMemberAttribute attribute to the members you want to serialize. For more information about data contracts, see Designing Service Contracts.

    internal class Person  
        internal string name;  
        internal int age;  

To serialize an instance of type Person to JSON

  1. Create an instance of the Person type.

    Person p = new Person(); = "John";  
    p.age = 42;  
  2. Serialize the Person object to a memory stream using the DataContractJsonSerializer.

    MemoryStream stream1 = new MemoryStream();  
    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Person));  
  3. Use the WriteObject method to write JSON data to the stream.

    ser.WriteObject(stream1, p);  
  4. Show the JSON output.

    stream1.Position = 0;  
    StreamReader sr = new StreamReader(stream1);  
    Console.Write("JSON form of Person object: ");  

To deserialize an instance of type Person from JSON

  1. Deserialize the JSON-encoded data into a new instance of Person by using the ReadObject method of the DataContractJsonSerializer.

    stream1.Position = 0;  
    Person p2 = (Person)ser.ReadObject(stream1);  
  2. Show the results.

    Console.WriteLine($"Deserialized back, got name={}, age={p2.age}");  


// Create a User object and serialize it to a JSON stream.  
public static string WriteFromObject()  
    //Create User object.  
    User user = new User("Bob", 42);  
    //Create a stream to serialize the object to.  
    MemoryStream ms = new MemoryStream();  
    // Serializer the User object to the stream.  
    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(User));  
    ser.WriteObject(ms, user);  
    byte[] json = ms.ToArray();  
    return Encoding.UTF8.GetString(json, 0, json.Length);  
// Deserialize a JSON stream to a User object.  
public static User ReadToObject(string json)  
    User deserializedUser = new User();  
    MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));  
    DataContractJsonSerializer ser = new DataContractJsonSerializer(deserializedUser.GetType());  
    deserializedUser = ser.ReadObject(ms) as User;  
    return deserializedUser;  


The JSON serializer throws a serialization exception for data contracts that have multiple members with the same name, as shown in the following sample code.

public class TestDuplicateDataBase  
    public int field1 = 123;  

public class TestDuplicateDataDerived : TestDuplicateDataBase  
    public new int field1 = 999;  

See Also

Stand-Alone JSON Serialization
Support for JSON and Other Data Transfer Formats