237

I am trying to return some JSON from a WCF service. This service simply returns some content from my database. I can get the data. However, I am concerned about the format of my JSON. Currently, the JSON that gets returned is formatted like this:

{"d":"[{\"Age\":35,\"FirstName\":\"Peyton\",\"LastName\":\"Manning\"},{\"Age\":31,\"FirstName\":\"Drew\",\"LastName\":\"Brees\"},{\"Age\":29,\"FirstName\":\"Tony\",\"LastName\":\"Romo\"}]"} 

In reality, I would like my JSON to be formatted as cleanly as possible. I believe (I may be incorrect), that the same collection of results, represented in clean JSON, should look like so:

[{
  "Age": 35,
  "FirstName": "Peyton",
  "LastName": "Manning"
}, {
  "Age": 31,
  "FirstName": "Drew",
  "LastName": "Brees"
}, {
  "Age": 29,
  "FirstName": "Tony",
  "LastName": "Romo"
}]

I have no idea where the “d” is coming from. I also have no clue why the escape characters are being inserted. My entity looks like the following:

[DataContract]
public class Person
{
    [DataMember]
    public string FirstName { get; set; }

    [DataMember]
    public string LastName { get; set; }

    [DataMember]
    public int Age { get; set; }

    public Person(string firstName, string lastName, int age)
    {
        this.FirstName = firstName;
        this.LastName = lastName;
        this.Age = age;
    }
}

The service that is responsible for returning the content is defined as:

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class TestService
{
    [OperationContract]
    [WebGet(ResponseFormat = WebMessageFormat.Json)]
    public string GetResults()
    {
        List<Person> results = new List<Person>();
        results.Add(new Person("Peyton", "Manning", 35));
        results.Add(new Person("Drew", "Brees", 31));
        results.Add(new Person("Tony", "Romo", 29));

        // Serialize the results as JSON
        DataContractJsonSerializer serializer = new DataContractJsonSerializer(results.GetType());
        MemoryStream memoryStream = new MemoryStream();
        serializer.WriteObject(memoryStream, results);

        // Return the results serialized as JSON
        string json = Encoding.Default.GetString(memoryStream.ToArray());
        return json;
    }
}

How do I return “clean” JSON from a WCF service? Thank you!

Mickael Lherminez
  • 679
  • 1
  • 10
  • 29
user208662
  • 10,869
  • 26
  • 73
  • 86
  • SOAP should return XML. You can use a REST endpoint to return JSON. Take a look http://stackoverflow.com/questions/186631/rest-soap-endpoints-for-a-wcf-service/186695#186695 – Akira Yamamoto Sep 15 '14 at 21:21
  • 4
    By the way, if anyone else comes across this and wonders why the "d" property is there, it's there to patch a [JSON vulnerability](http://haacked.com/archive/2008/11/20/anatomy-of-a-subtle-json-vulnerability.aspx/). Removing it makes you vulnerable again. – Alex Feb 02 '15 at 14:14
  • 4
    @Alex - that Vulnerability depends on redefining the Array object, which is no longer possible in modern browsers. See http://stackoverflow.com/questions/16289894/is-json-hijacking-still-an-issue-in-modern-browsers – Cheeso Feb 20 '15 at 20:52
  • That's good. :) Half my answer is still true though -- it WAS there to patch that vulnerability. – Alex Feb 22 '15 at 10:53

6 Answers6

216

Change the return type of your GetResults to be List<Person>.
Eliminate the code that you use to serialize the List to a json string - WCF does this for you automatically.

Using your definition for the Person class, this code works for me:

public List<Person> GetPlayers()
{
    List<Person> players = new List<Person>();
    players.Add(new  Person { FirstName="Peyton", LastName="Manning", Age=35 } );
    players.Add(new  Person { FirstName="Drew", LastName="Brees", Age=31 } );
    players.Add(new  Person { FirstName="Brett", LastName="Favre", Age=58 } );

    return players;
}

results:

[{"Age":35,"FirstName":"Peyton","LastName":"Manning"},  
 {"Age":31,"FirstName":"Drew","LastName":"Brees"},  
 {"Age":58,"FirstName":"Brett","LastName":"Favre"}]

(All on one line)

I also used this attribute on the method:

[WebInvoke(Method = "GET",
           RequestFormat = WebMessageFormat.Json,
           ResponseFormat = WebMessageFormat.Json,
           UriTemplate = "players")]

WebInvoke with Method= "GET" is the same as WebGet, but since some of my methods are POST, I use all WebInvoke for consistency.

The UriTemplate sets the URL at which the method is available. So I can do a GET on http://myserver/myvdir/JsonService.svc/players and it just works.

Also check out IIRF or another URL rewriter to get rid of the .svc in the URI.

Cheeso
  • 189,189
  • 101
  • 473
  • 713
  • Cheeso - I tried this approach before I posted this question. When I use this approach, I get an error that says "Endpoints using 'UriTemplate' cannot be used with 'System.ServiceModel.Description.WebScriptEnablingBehavior'." What am I doing wrong? Thank you! – user208662 Jan 19 '10 at 13:09
  • 30
    use instead of in your .config file. – Cheeso Jan 19 '10 at 13:20
  • I'm getting the "endpoints using ..." error above but don't have in my web.config anywhere. where do I put ? – MGOwen Nov 23 '10 at 04:35
  • 9
    OK, I replaced **** with **** and that worked. – MGOwen Nov 23 '10 at 04:39
  • 3
    MGowen - FYI, best bet when asking a new question is to ...open a new question, rather than posting the question as a comment to an old answer. – Cheeso Nov 23 '10 at 19:56
  • 5
    Favre sees what you did there. – ruffin Feb 27 '12 at 19:45
  • Did not work for me. I am still getting a list in my test applicaiont and xml in the WCF test client. What changes need to be made in the config file? – user20358 Oct 12 '12 at 10:57
  • @Cheeso what do you return your object then as just a string cause mine doesnt have players in it just the data in json format – c-sharp-and-swiftui-devni May 25 '19 at 19:55
94

If you want nice json without hardcoding attributes into your service classes,

use <webHttp defaultOutgoingResponseFormat="Json"/> in your behavior config

JeremyWeir
  • 24,118
  • 10
  • 92
  • 107
28

This is accomplished in web.config for your webservice. Set the bindingBehavior to <webHttp> and you will see the clean JSON. The extra "[d]" is set by the default behavior which you need to overwrite.

See in addition this blogpost: http://blog.clauskonrad.net/2010/11/how-to-expose-json-endpoint-from-wcf.html

user517301
  • 281
  • 3
  • 2
8

I faced the same problem, and resolved it by changing the BodyStyle attribut value to "WebMessageBodyStyle.Bare" :

[OperationContract]
[WebGet(BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json, UriTemplate = "GetProjectWithGeocodings/{projectId}")]
GeoCod_Project GetProjectWithGeocodings(string projectId);

The returned object will no longer be wrapped.

AcAnanth
  • 765
  • 3
  • 19
  • 53
KhalilG
  • 201
  • 5
  • 13
2

When you are using GET Method the contract must be this.

[WebGet(UriTemplate = "/", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
List<User> Get();

with this we have a json without the boot parameter

Aldo Flores @alduar http://alduar.blogspot.com

Markus Jarderot
  • 86,735
  • 21
  • 136
  • 138
alduar
  • 71
  • 1
  • 5
1

In your IServece.cs add the following tag : BodyStyle = WebMessageBodyStyle.Bare

 [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "Getperson/{id}")]

    List<personClass> Getperson(string id);
Osama Ibrahim
  • 995
  • 10
  • 13