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 DeSerialize Json file

(0) ShareShare
ReportReport
Posted on by 570

Hi All,

I have JSON file, please find blow . I  want to read and insert in d365fo 

I follow  below link to resolve but no luck, please give solution for this 

community.dynamics.com/.../deserialize-json-build-from-an-array-of-values-in-d365fo

[
  {
    "id": 1,
    "name": "Leanne Graham",
    "username": "Bret",
    "email": "Sincere@april.biz",
    "address": {
      "street": "Kulas Light",
      "suite": "Apt. 556",
      "city": "Gwenborough",
      "zipcode": "92998-3874",
      "geo": {
        "lat": "-37.3159",
        "lng": "81.1496"
      }
    },
    "phone": "1-770-736-8031 x56442",
    "website": "hildegard.org",
    "company": {
      "name": "Romaguera-Crona",
      "catchPhrase": "Multi-layered client-server neural-net",
      "bs": "harness real-time e-markets"
    }
  },

I have the same question (0)
  • Suggested answer
    Sergei Minozhenko Profile Picture
    23,093 on at

    Hi Tony,

    I haven't deserialized JSON to an array, but I deserialized it to contract.

    First, you need to create a contract class for root information and for all nested nodes like address, geo, company contract should be created too in the same way.

    [DataContract]
    class YourRootContractClass
    {
        private str id;
        ...
        private AddressContract address; //Create new contract for address fields
        ...
        private str phone;
    
        [DataMember("id")]
        public str parmId(str _id = id)
        {
            id = _id;
            return id;
        }
        
        ..
        [DataMember("address")]
        public AddressContract parmAddress(AddressContract _address = address)
        {
            address = _address;
            return address;
        }
        
        ...
    
        [DataMember("phone")]
        public str parmPhone(str _phone = phone)
        {
            phone = _phone;
            return phone;
        }
    
    }

    And deserialize it

    YourRootContractClass contract = FormJsonSerializer::deserializeObject(classId(YourRootContractClass), json_value);

    Access variable from contract via parm methods

    info(contract.parmId());

  • WillWU Profile Picture
    22,361 on at

    Hi Tony,

    You can also have a look at the RetailCommonWebAPI class.

    Map map = RetailCommonWebAPI::getMapFromJsonString(json);

    str = map.lookup('KEY');

    See the blog:

    https://allaboutmsdynamics.wordpress.com/2018/12/18/d365-ax7read-parse-json-javascript-object-notation/

    Hope this helps.

  • TonyAx Profile Picture
    570 on at

    Hi Sergei,

    Thanks for reply, your code is working but its task  first record only.

    please check below JSON data on that name are two .

    {

    "id": 1,
        "name": "Leanne Graham",
        "username": "Bret",
        "email": "Sincere@april.biz",
        "address":

    }

    {

    "id": 2,
        "name": "Ervin Howell",
        "username": "Antonette",
        "email": "Shanna@melissa.tv"

    }

  • Sergei Minozhenko Profile Picture
    23,093 on at

    Hi Tony,

    Try to create one more (root) contract to put customer contract to the list and use it in deserialize operation.

    [DataContract]
    class CustomerListContract
    {
        List customerList = new List(Types::String);
        
        [
            DataMemberAttribute("CustomerList"), 
            DataCollectionAttribute(Types::Class, classStr(CustomerContract))
        ]
        public List parmCustomerList(List _customerList = customerList)
        {
            if (!prmIsDefault(_customerList))
            {
                customerList = _customerList;
            }
    
            return customerList;
        }
    
    }

  • Suggested answer
    Sukrut Parab Profile Picture
    71,710 Moderator on at

    Hi Tony ,

    Your requirement can be achieve using two contract classes. Take a look at examples from from below link might help you.

    community.dynamics.com/.../serializing-and-deserializing-json-in-ax

  • Hassan najam Profile Picture
    30 on at

    Hi 

    Its not working in my case.

    I have JSON like this 

    {
    "status": "OK",
    "message": "Good",
    
        "taxRefundResponse": {
        "taxRefundTagNumber": "T00001",
        
        }
    
    }

    I created two contract class:

    This is main contract

    [DataContractAttribute]
    public class PGContract
    {
    
        str    status,code;
        PGtaxRefundResponseContract taxRefundResponse;
    
        [DataMemberAttribute('status')]
        public str parmstatus(str _month = status)
        {
            status = _month;
            return status;
        }
    
        [DataMemberAttribute('code')]
        public str parmcode(str _month = code)
        {
            code = _month;
            return code;
        }
    
        [DataMember("taxRefundResponse"),DataCollectionAttribute(Types::class, classStr(PGtaxRefundResponseContract))]
        public PGtaxRefundResponseContract parmtaxRefundResponse(PGtaxRefundResponseContract _parmtaxRefundResponse = taxRefundResponse)
        {
            taxRefundResponse = _parmtaxRefundResponse;
            return taxRefundResponse;
        }
    
    }

    For inner object

    [DataContractAttribute]
    class PGtaxRefundResponseContract
    {
        str parmtaxRefundTagNumber;
    
        [DataMemberAttribute('taxRefundTagNumber')]
        public String50 parmtaxRefundTagNumber(String50 _parmtaxRefundTagNumber = parmtaxRefundTagNumber)
        {
            parmtaxRefundTagNumber = _parmtaxRefundTagNumber;
            return parmtaxRefundTagNumber;
        }
    
    }

    When I debug it give me null value 

    pastedimage1678781685379v1.png

    Would you like share your opinion?

  • Martin Dráb Profile Picture
    237,948 Most Valuable Professional on at

    First of all, fix the input, because it's not valid JSON. Even the editor here indicates an error. The bug is at line six; you shouldn't have the comma at the end.

    Also, your PGContract doesn't have any member called 'message' (it's called 'code' instead).

  • Hassan najam Profile Picture
    30 on at

    {
    "status": "OK",
    "code": "Good",
    
        "taxRefundResponse": {
        "taxRefundTagNumber": "T00001"
        
        }
    
    }

    Yes goshoom now correct the JSON.

  • Suggested answer
    Hassan najam Profile Picture
    30 on at

    Hi 

    After debug I found the solution how to get values of child JSON object.

    YourContactClass yourContactClass = FormJsonSerializer::deserializeObjectStrict(classNum(YourContactClass), jsonString);

    Thanks.

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
Martin Dráb Profile Picture

Martin Dráb 559 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 250 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans