Forum Discussion

miraks's avatar
miraks
Ideator I
5 years ago

How to return JSON from a odata method

Hi,

I created a a serveur method named COCO_MONITORING returning a JSON like this

var innovator = this.getInnovator();
var output= new Newtonsoft.Json.Linq.JObject();
....
return innovator.newResult(output.ToString(Newtonsoft.Json.Formatting.None));

I'm able to call it in odata like this:
https://serveur/1Z62innovator/server/odata/method.COCO_MONITORING

My problem is that the output of the web service is a text/plain containing the json as a text.

I would like to have directly the json (application/json).

How can I do that ?

3 Replies

  • Hi Miraks,

    newResult can just return text. Maybe you can add your output to a temporary Aras item and then return the item?

    • miraks's avatar
      miraks
      Ideator I

      Hi Angelalp,

      Thank you for your answer.

      If my method is like this:

      return this.getInnovator().newResult(new Newtonsoft.Json.Linq.JObject
      {
      {"return_code", 0} ,
      {"message", "hello world!"}
      }.ToString(Newtonsoft.Json.Formatting.None));

      The output is a text:

      If my method is like this:

      var item = this.getInnovator().newItem();
      item.setProperty("json", new Newtonsoft.Json.Linq.JObject
      {
      {"return_code", 0} ,
      {"message", "hello world!"}
      }.ToString(Newtonsoft.Json.Formatting.None));

      return item;

      The output is a text:

      So, you are right, with your solution the output is well a JSON.

      But a JSON of an Item. Is it possible to send a fully customized JSON ?

      Regards.

      • AngelaIp's avatar
        AngelaIp
        Ideator I

        Hi Miraks,

        standard Aras Method just can return items. "Results" and "Error" are also just items.

        Maybe you can use some kind of custom class or other more native C# techniques, but I am not sure. Some times you see these kind of designs in native Aras code.