wil
wil

Reputation: 903

how to return json format from ODATA?

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?

Upvotes: 35

Views: 105411

Answers (10)

user2581078
user2581078

Reputation: 1

the URL should look like this https://blabla.com/OdataService?$format=json it's work for me

Upvotes: -3

Radosław Kiela
Radosław Kiela

Reputation: 21

... just use lower case letters:

"format=json"

Upvotes: 1

Nate Cook
Nate Cook

Reputation: 8585

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.

Upvotes: 13

user1781186
user1781186

Reputation:

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'

Upvotes: 1

zackdever
zackdever

Reputation: 1642

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>
{
     ...

Upvotes: 4

Oliver Gray
Oliver Gray

Reputation: 874

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>

Upvotes: 9

Steven Veltema
Steven Veltema

Reputation: 2150

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();  

Upvotes: 0

lamarant
lamarant

Reputation: 3390

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/

Upvotes: 27

user4816271
user4816271

Reputation: 21

"...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)

Upvotes: 2

Glaxalg
Glaxalg

Reputation: 684

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

Check out this link

Upvotes: 7

Related Questions