web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

How to send response in Json from D365FO for RestAPI

(3) ShareShare
ReportReport
Posted on by 230
Hi Team,
 
I have to send response in json like below
{
  "statusCode": "200", “resultStatus”:success
}
 
For that i am using below contract class 
[DataContract]
class ERPDataContract
{
    Str statusCode;
   
    [DataMember("statusCode")]
    public str parmstatusCode(Str _statusCode = statusCode)
    {
        statusCode= _statusCode;
        return statusCode;
    }
Below is my service class
Class ERPDataService
{
  public str process()
  {
    str jsonString = ("statusCode: 200, resultStatus:success");
    ERPDataContract contract = new ERPDataContract();

   contract.parmStatusCode(jsonString);
   jsonString = FormJsonSerializer::serializeClass(contract);
    info(jsonString);
   return jsonString;
  }
}
Below info i am getting.
{"statusCode": "statusCode: 200, resultStatus:success"}
 
Please let me know how to send response like
{
  "statusCode": "200", “resultStatus”:success
}
I have the same question (0)
  • Suggested answer
    Martin Dráb Profile Picture
    237,990 Most Valuable Professional on at
    200 is an HTTP status code meaning that the request was successful. It's already returned by your custom service, therefore duplicating this information inside the response body is useless. If they really require that, I would let them know that it's not a good design.
     
    But of course that is can be done if they insist. Don't bother trying to create the JSON string by yourself; F&O will do it for you. Simply return the contract object from your service method:
    public ERPDataContract process()
    {
        ERPDataContract contract = new ERPDataContract();
    
        contract.parmStatusCode(200);
        contract.parmResultStatus('success');
    
        return contract;
    }
    The contract class needs two properties you want to return: statusCode and resultStatus.
     
    (Moved from Dynamics AX forum.)
  • CU21091228-0 Profile Picture
    230 on at
    In case of returning contract object, below error i am getting.
     
    Exception occurs when serializing the return value-Unrecognized AX collection type.
     
    I am getting this error in postman while post the service.
  • Martin Dráb Profile Picture
    237,990 Most Valuable Professional on at
    Please show us your current code. It sounds like you used a collection there (which isn't needed) and you did it wrong, but I can only guess until you tell us what you did.
  • Suggested answer
    Anton Venter Profile Picture
    20,346 Super User 2025 Season 2 on at
    Like Martin mentioned, add a resultStatus member to your contract because it's missing and required in your response JSON. I think the serialization of the contract is where the issue is.
     
    [DataContract]
    class ERPDataContract
    {
        str statusCode, resultStatus;
       
        [DataMember("statusCode")]
        public str parmstatusCode(str _statusCode = statusCode)
        {
            statusCode= _statusCode;
            return statusCode;
        }
    	
        [DataMember("resultStatus")]
        public str parmResultStatus(str _resultStatus = resultStatus)
        {
            resultStatus = _resultStatus;
            return resultStatus;
        }
    }
    and then set the resultstatus member separately.
       ERPDataContract contract = new ERPDataContract();
    
       contract.parmStatusCode("200");
       contract.parmResultStatus("success");
       
       jsonString = FormJsonSerializer::serializeClass(contract);	


     
  • CU21091228-0 Profile Picture
    230 on at
    Below is my code.
    class ERPDataService
    {
    
    
        [AifCollectionTypeAttribute('return', Types::Class, classStr(ERPDataResponceContract)),
            AifCollectionType('tables', Types::String)]
        public ERPDataResponceContract process(list tables, str id, boolean debug)
        {
            ListIterator tableNameIterator;      
            ERPDataResponceContract responceContract = new ERPDataResponceContract();
    
            try
            {
                tableNameIterator = new ListIterator(tables);
                 while (tableNameIterator.more())
                 {
                   // Get the value
    	     }
                responceContract.parmStatusCode('200');
                responceContract.parmrRsultStatus('success');
                
            }
                 
            catch(Exception::Error)
            {
               ////
               ///
                      
            }
         return responceContract;
    }
     
  • Suggested answer
    Martin Dráb Profile Picture
    237,990 Most Valuable Professional on at
    Throw away the first instance of AifCollectionType attribute. It claims that your service returns a collection of ERPDataResponceContract objects, which isn't true. It returns a single ERPDataResponceContract object only and therefore specifying the type of objects in the (non-existing) collection makes no sense.
  • CU21091228-0 Profile Picture
    230 on at
    I have removed below code.
    AifCollectionTypeAttribute('return', Types::Class, classStr(ERPDataResponceContract))
    Now i am getting below response.
     {
       "$id":"1", "statusCode": "200", “resultStatus”: "success" 
    }
     
    Not sure how "$id":"1" is coming it is not required.
  • Suggested answer
    Martin Dráb Profile Picture
    237,990 Most Valuable Professional on at
    Decorate your response contract class with Newtonsoft.Json.JsonObjectAttribute(IsReference = false).
     
    You'll find blog posts with concrete examples, such as Remove $id from Custom Web Service JSON – Dynamics 365 F&O.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Abhilash Warrier Profile Picture

Abhilash Warrier 669 Super User 2025 Season 2

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 471 Super User 2025 Season 2

#3
Martin Dráb Profile Picture

Martin Dráb 423 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans