Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 Community / Forums / Finance forum / How to deserialize a J...
Finance forum
Suggested answer

How to deserialize a JSON string in D365fo

Posted on by 195

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

  • Martin Dráb Profile Picture
    Martin Dráb 225,610 Super User on at
    RE: How to deserialize a JSON string in D365fo

    Hi Khadar, your problem isn't the same as the Zohan_93's (the author of this thread), therefore it doesn't belong here.

    Please create a new thread and explain your particular problem there. Please add also the information about the contact classes that you're,trying to deserialize the JSON string to.

    Use Insert > Code (in the frich formatting view) to paste source code to this forum.

  • khadar shaik Profile Picture
    khadar shaik 55 on at
    RE: How to deserialize a JSON string in D365fo

    Hi Martin,

    below is my json string

    {

     "currentPage": 1,

     "totalPages": 1,

     "totalItems": 18,

     "itemsPerPage": 2,

     "items": [

       {

         "bookingReference": "46000846",

         "bookingCustomerId": "28183807",

         "bookingDate": "2021-09-22",

         "bookingStatus": "Paid",

         "bookingLocation": "Consumer",

         "productId": "281064",

         "bookingDiscountIds": [],

         "modifiers": [],

         "bookingCreatedByStaffId": null

       },

       {

         "bookingReference": "46000846",

         "bookingCustomerId": "28183807",

         "bookingDate": "2021-09-22",

         "bookingStatus": "Paid",

         "bookingLocation": "Consumer",

         "productId": "526955",

         "bookingDiscountIds": [],

         "modifiers": [],

         "bookingCreatedByStaffId": null

       }

     ]

    }

    I am facing 'The type of object cannot be set.' error while using 'FormJsonSerializer' class

    responseList = FormJsonSerializer::deserializeCollection(classnum(List), responseJson, Types::Class, classStr(ResponseRootContract));

  • Martin Dráb Profile Picture
    Martin Dráb 225,610 Super User on at
    RE: How to deserialize a JSON string in D365fo

    LuckSong's problem was resolved in another thread: How to deserialize a JSON string.

  • LuckSong Profile Picture
    LuckSong 75 on at
    RE: How to deserialize a JSON string in D365fo

    hi Zohan:

     At last, how did you solve the error"Object The type of object cannot be set."?  l has the same issue.

  • Zohan_93 Profile Picture
    Zohan_93 195 on at
    RE: How to deserialize a JSON string in D365fo

    Thanks everyone, I've fulfill my requirements by getting all the suggestion.

    Regards,

    Zohan

  • Zohan_93 Profile Picture
    Zohan_93 195 on at
    RE: How to deserialize a JSON string in D365fo

    Sergei,

    I've a created a contract class with all possible values with type str, for example, id, type etc. then after your suggestion i've created another contract class with type list,and create a method of lines with type list as per your last thread, now when i try to deserialize this by using

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

    , i got an exception of "The type of object cannot be set"....".....

  • Martin Dráb Profile Picture
    Martin Dráb 225,610 Super User on at
    RE: How to deserialize a JSON string in D365fo

    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.

  • Sergei Minozhenko Profile Picture
    Sergei Minozhenko 23,083 on at
    RE: How to deserialize a JSON string in D365fo

    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));

  • Zohan_93 Profile Picture
    Zohan_93 195 on at
    RE: How to deserialize a JSON string in D365fo

    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?

  • Zohan_93 Profile Picture
    Zohan_93 195 on at
    RE: How to deserialize a JSON string in D365fo

    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\"

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,995 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,610 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans