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 a JSON string in D365fo

(0) ShareShare
ReportReport
Posted on by 199

Hi,

I've consumed a json string from webapi, now i want to deserialize a json string in x++ to insert the values in custom table. I'm getting json string in this format

"[{\"id\":3,\"Name\":\"abc\",\"Reference\":\"T12345\"}]"

Anyone with verified solution kindly do suggest as I've tried some solutions but it doesn't fulfill my requirements. Please Note i've to achieve this via x++ not c#.

Regards,

Zohan

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

    Hi,

    You can use the contracts and FormJsonSerializer class

    community.dynamics.com/.../how-to-deserialize-json-file-in-d365fo

  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    Since we don't know which solutions you tried, it might be that we suggest something that doesn't work for you. If you wish to avoid that, you can tell us which solutions you tried.

    Normally you would develop a data contract in C#  and use some existing library such as NewtonSoft.Json to handle it. Then build the whole thing to DLL and use it from your x++ code.

    Here's one example: msdax.wordpress.com/.../

    I don't know why you want to avoid using C# but if that's really a hard requirement for you, unfortunately I can't help you.  But where does that requirement come from? I suggest to discuss with that person because I don't see any business reason to not use it.

  • Zohan_93 Profile Picture
    199 on at

    Thankyou sergei, but what if my string contains multiple records , then should i use list instead of string in contract class ? if yes then it throws an exception on

    contract = FormJsonSerializer::deserializeObject(classNum(WebApiContract),_jsonString);

    "The type of object cannot be set"..

    any suggestion ?

  • nmaenpaa Profile Picture
    101,160 Moderator on at

    Could you share the actual JSON string? There were no multiple records in the example that you shared in your first post. So we can't see what is the format of your actual JSON.

  • Sergei Minozhenko Profile Picture
    23,093 on at

    Hi Zohan_93,

    Try to make a root contract that will contain a List of contracts for one record only and use it as a parameter for deserializeObject method. Check the example in my last comment in the thread mentioned above.

  • Suggested answer
    Sergei Minozhenko Profile Picture
    23,093 on at

    Hi Zohan_93,

    Or you can try to deserialize it as collection:

    List listOfcontract = FormJsonSerializer::deserializeCollection(classNum(List), json_string, types::Class, classStr(YourContract));

  • Zohan_93 Profile Picture
    199 on at

    Here's is the string

    "[{\"id\":3,\"tType\":\"payment\",\"referenceNo\":\"1533\",\"date\":\"2020-03-17T00:00:00\",\"amount\":100000.00,\"debit\":\"2205\",\"credit\":\"IN\",\"remarks\":\"TR No 855-1533/76/7-2\",\"status\":\"read count 21\"},{\"id\":4,\"Type\":\"payment\",\"referenceNo\":\"1579\",\"date\":\"2020-03-17T00:00:00\",\"amount\":80000.00,\"debit\":\"50001\",\"credit\":\"IA\"

  • Zohan_93 Profile Picture
    199 on at

    Sergei,

    here's the code ,

    [

           DataMember("Id"),

           DataCollectionAttribute(Types::Class, classStr(WebApiContract))

       ]

       public List parmId(List _id = id)

       {

           id = _id;

           return id;

       }

    }

    now when i try to achieve this by using //contract = FormJsonSerializer::deserializeObject(classNum(WebApiContract),_jsonString); it throws me an exception of The type of object cannot be set"....

    any suggestion?

  • Sergei Minozhenko Profile Picture
    23,093 on at

    Hi Zohan_93,

    Is WebApiContract your contract for one record? Could you, please, share it as well?

    Also, try deserializeCollection method.

    List listOfcontract = FormJsonSerializer::deserializeCollection(classNum(List), _jsonString, types::Class, classStr(WebApiContract));

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

    Let's start by converting the escaped string to actual JSON and format it, so we can see the structure.

    [{
    	"id":3,
    	"tType":"payment",
    	"referenceNo":"1533",
    	"date":"2020-03-17T00:00:00",
    	"amount":100000.00,
    	"debit":"2205",
    	"credit":"IN",
    	"remarks":"TR No 855-1533/76/7-2",
    	"status":"read count 21"
    },
    {
    	"id":4,
    	"Type":"payment",
    	"referenceNo":"1579",
    	"date":"2020-03-17T00:00:00",
    	"amount":80000.00,
    	"debit":"50001",
    	"credit":"IA"

    It's now obvious that the string is incomplete.
    Also, your contract class is for an object with a property "Id" containing the list of other objects, but there is nothing like that in your JSON. And then you don't use it for deserialization anyway; your code is for deserialization of a single WebApiContract object.

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 660 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 307 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans