cancel
Showing results for 
Search instead for 
Did you mean: 

How to return deep json object in odata api

hod222
Discoverer
0 Kudos
226
  • SAP Managed Tags:

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?

hod222
Discoverer
0 Kudos

Thank u but I use v2,

The object i want to get in get api  is like this

Data : { date: '13.08.2024',

        Booleanfields :true,

       Table1:[ { itemType :1,

                       Table2 :[{ name:'TTT', 

                                       Cost:34 }....], //end of table 2

                       Table3:[ { name:'ee',

                                     Table4:[ { age:4,

                                                           Booleanfield : false }....] // end of table4

                                     }...] // end of table 3

                   }...] // end of table1

      } // end of data

I not shure if it possiable to use get expanded entity hear and if to be honest i dont realy want because i need to do few get apis that return diffrent but similar data like above ( implementing $expended entity is not easy )

There is simple way to do that in v2? Is it possible in rest api?

Accepted Solutions (0)

Answers (2)

Answers (2)

RalfHandl
Product and Topic Expert
Product and Topic Expert
0 Kudos

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.

RalfHandl
Product and Topic Expert
Product and Topic Expert
0 Kudos

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?