Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to return json format from ODATA?

Tags:

json

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?

like image 542
wil Avatar asked Aug 12 '10 14:08

wil


People also ask

Can OData return JSON?

This pattern ensures JSON payloads returned from OData services are valid JSON statements, but not valid JavaScript statements.

How do I get metadata from OData?

You can use jQuery to get the relevant information from an OData service $metadata. Take for example: You write a unit test to check the OData entities property names matches with your application entities. Then you have to retrieve the properties of the OData entity.

How do I query OData service?

You can find details on filter specification in the OData spec filter options section. Examples: All products with a Name equal to 'Milk': http://host/service/Products?$filter=Name eq 'Milk' All products with a Name not equal to 'Milk' : http://host/service/Products?$filter=Name ne 'Milk'


2 Answers

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/

like image 183
lamarant Avatar answered Sep 20 '22 01:09

lamarant


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.

like image 34
Nate Cook Avatar answered Sep 22 '22 01:09

Nate Cook