Parsing OData metadata in JavaScript

2.7k Views Asked by At

Following the example provided here: http://datajs.codeplex.com/wikipage?title=Metadata%20Generation%20Sample&referringTitle=Documentation

I retrieve metadata in the form:

{"version":"1.0","dataServices":{"dataServiceVersion":"1.0","schema":[{"namespace":"IdeasDbModel","entityType":[{"name":"Category","key":{"propertyRef":[{"name":"CategoryID"}]},"property":[{"name":"CategoryID","type":"Edm.Int32","nullable":"false","extensions":[{" ...

How can I parse this in JavaScript to retrieve metadata information such as column type, primary key, etc for example table Category has primary key (propertyRef) CategoryID and that column is of type Edm.Int32? I don't understand the format of the results. Under dataServices -> Schema[0] -> entityType (or association), it is just an array of objects so do I have to loop through every single element to find a matching result or is there an easier way to have the columns keyed by name?

1

There are 1 best solutions below

0
On

You should look at JayData. It creates a JavaScript class context from the metadata, that you can use more intuitively. An example after referencing JayData in your page:

$data.service("http://localhost:60349/Northwind.svc", function (f, t) {
 var northwind = f();
 console.dir(northwind["Categories"].elementType.memberDefinitions["$Category_ID"]);
});

The result:

computed: true
configurable: true
dataType: "Edm.Int32"
definedBy: function Category(){
enumerable: true
key: true
kind: "property"
name: "Category_ID"
nullable: false
type: "Edm.Int32"