cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

How to return deep json object in odata api

hod222
Explorer
0 Likes
2,054

I need create an api that will return structure that one of is fields is table base on structure that one of his fields is table too.

implementing $expended entity is too much complicated

what i do now is return a string

and in the front end i do json parse ( and it is not comfort because i need run over all boolean values and convert 'x' to true... )

can i return json object without instriction of flat entity?

Accepted Solutions (1)

Accepted Solutions (1)

RalfHandl
Product and Topic Expert
Product and Topic Expert
0 Likes

There is no way to do that in OData V2, other than what you have already discovered: tunnel your deep structure in a JSON string.

OData V2 does not support collection-valued properties, which is one of the many reasons why we moved forward to OData V4 ten years ago.

Maybe you want to switch to V4.

Answers (1)

Answers (1)

RalfHandl
Product and Topic Expert
Product and Topic Expert
0 Likes

With OData V4 you can model deep json objects with complex types and collections of complex types:

<EntityType Name="X">
  <Key>
    <PropertyRef Name="id"/>
  </Key>
  <Property Name="id" Type="Edm.String" Nullable="false"/>
  <Property Name="data" Type="test.Y"/>
  <Property Name="deepData" Type="Collection(test.Y)" Nullable="true"/>
</EntityType>
<ComplexType Name="Y">
  <Property Name="foo" Type="Edm.String"/>
  <Property Name="bar" Type="test.Z"/>
  <Property Name="baz" Type="Collection(test.Z)" Nullable="true"/>
</ComplexType>
<ComplexType Name="Z">
  <Property Name="x" Type="Edm.String"/>
  <Property Name="y" Type="Edm.String"/>
</ComplexType>

In JSON "data" will be an object, "deepData" an array of objects, and in both cases the objects have properties of type string, object, and array of object, respectively.

In CAP CDS the corresponding model would be

entity X {
  key id   :      String;
  data     :      Y;
  deepData : many Y;
}

type Y {
  foo :      String;
  bar :      Z;
  baz : many Z;
}

type Z {
  x : String;
  y : String;
}

Does this answer your question?

hod222
Explorer
0 Likes

Thank u I decided to use odata v4 .

how can i create collection in segw? And i need to use getEntity?

I've been searching the internet for guides on v4 and can't find any.