35

I know ODATA can return json but not sure if I have to use an attribute or interface to do so.

I want it to do just like http://odata.netflix.com/Catalog/Titles?$format=JSON but my odata service doesn't return JSON. When I call it like www.foo.com/service?$format=json, it just returns XML.

What do I need to do to return json with ODATA?

Pankaj
  • 888
  • 10
  • 11
wil
  • 853
  • 2
  • 10
  • 24
  • It's a hack, but if you want to return JSON by default, in your ODataConfig (or WebApiConfig) under App_Start you can add this line of code to the bottom of your `Register` method: `config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));`. – BrainSlugs83 Jun 20 '15 at 00:23
  • 2
    idk but I have to write `json` all in lowercase and then it works. Maybe CaSeSeNsItIvE? – inetphantom Jan 04 '16 at 14:03
  • Same result here, I just needed to write `json` in lowercase. Alternatively, setting the request header to `Accept: application/json` also worked – Jonathan Benn Jun 09 '16 at 19:20
  • 2
    For me works adding this to my url: ?&$format=json – Cesar Miguel May 22 '17 at 16:51

9 Answers9

27

Download and install Fiddler.

http://www.fiddler2.com/fiddler2/

Once installed, open it, click on the "Request Builder" tab located in the right side of Fiddler.

Insert this URL:

http://test.com/feed2/ODataService.svc/results

Note that you DO NOT NEED THE ?$format=JSON

In the "Request Headers" section, insert the following line:

accept: application/json

Hit the Big "Execute" button at the top right of Fiddler.

You'll see the results of the request added to the list on the left side of Fiddler.

Double click on the request. The right side of Fiddler will change to the "Inspectors" tab where you can see the results of your request.

Also, since you are working with Json, you probably want to download and install the Json viewer plugin for Fiddler:

http://jsonviewer.codeplex.com/

lamarant
  • 3,243
  • 2
  • 25
  • 30
13

Newer versions of WCF Data Services support JSON by default and you must have

Accept: application/json;odata=verbose

in the request header.

Accept: application/json

is no longer sufficient. More info here.

Nate Cook
  • 8,395
  • 5
  • 46
  • 37
9

No-one seems to be answering your question very cleanly here!

From an HTML page you can use the following Javascript / JQuery code to have a WCF Data Service return data in JSON format;

    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script language="javascript" type="text/javascript">

        var sURL = "http://YourService.svc/Books(10)";

        function testJSONfetch() {

            $.ajax({
                type: "GET",
                contentType: "application/json; charset=utf-8",
                datatype: "json",
                url: sURL,
                error: bad,
                success: good,
                beforeSend: function (XMLHttpRequest) {
                    //Specifying this header ensures that the results will be returned as JSON.
                    XMLHttpRequest.setRequestHeader("Accept", "application/json");
                }
            });

        }

        function good(response)
        {

        }

        function bad(response) 
        {

        }

    </script>
Oliver Gray
  • 874
  • 6
  • 17
7

You need to add “Accept: application/json” into the request header section.

Check out this link

Glaxalg
  • 584
  • 6
  • 18
4

If you're using the ODATA provider from Data Services you can easily return ODATA as JSON by specifying it in the URL as in the sample you gave - http://odata.netflix.com/Catalog/Titles?$format=JSON

To do this use the JSONp and URL-controlled format support for ADO.NET Data Services download from MSDN http://code.msdn.microsoft.com/DataServicesJSONP and add the JSONPSupportBehavior decorator to your DataService class like below.

[JSONPSupportBehavior]
public class MyDataService : DataService<MyContextType>
{
     ...
zackdever
  • 1,642
  • 1
  • 13
  • 22
2

"...but I get "The webpage cannot be found" using http://test.com/feed2/ODataService.svc/results?$format=JSON ..."

you dont need the $format=JSON in the Uri.

Just use "http://test.com/feed2/ODataService.svc/results"

(with Accept: application/json in the request header)

1

Late answer, but I've been spending the last hour trying to figure out how to curl OData APIs and return the result as json. The following code fetches the document in json and writes it to a file:

-o myfile.html -H "Accept: application/json" http://example.com/api/data?$filter=name eq 'whatever'
1

... just use lower case letters:

"format=json"

  • That does not work. Solution provided by @Cesar Miguel works. Add ?&$format=json in the URL – Gana Oct 14 '20 at 12:00
0

It's not pretty but this is how I forced JSON output without using $format in the request string:

    Request r = new Request(Method.GET, "http://XXXXXXX.svc//Login"
                 + "&UserId=" + "'" + "user" + "'" 
                 + "&Password=" + "'" + "password" + "'");

    ClientInfo ci = r.getClientInfo();
    ArrayList<Preference<MediaType>> accepted = new ArrayList<Preference<MediaType>>();
    accepted.add(new Preference<MediaType>(MediaType.APPLICATION_JSON));
    ci.setAcceptedMediaTypes(accepted);

    Client client = new Client(Protocol.HTTP);  
    Response response = client.handle(r);  
    Representation output = response.getEntity();  
Steven Veltema
  • 2,140
  • 15
  • 18