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

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Unanswered

Deserialize JSON using C# dll file in x++

(0) ShareShare
ReportReport
Posted on by 384

Dears,

I want to deserialize a JSON object in X++ using an external C# dll file.

I would appreciate any helpful links.

I have the same question (0)
  • Martin Dráb Profile Picture
    236,172 Most Valuable Professional on at
    RE: Deserialize JSON using C# dll file in x++

    There is no magic way to convert JArray to a data contract class (or a collection of them). You need to go through everything in the object and assign it to right properties of right objects in your X++ code. For example, if you have a JProperty object with Name == "Value1", you'll assign its value to myContract.parmValue1(), or something like that.

    If you want to automatic deserialization to an object, you'll need to use another approach (such us what I suggested before).

  • sylvesterPowerBi Profile Picture
    384 on at
    RE: Deserialize JSON using C# dll file in x++

    deserializing the Json to JArray is totally working fine, and i can see its values in myArray.values()

    but how i can Map those values to my contract class in order to identity each value for each parameter,

    or in another meaning  i need to match the parameters in my contract class to the JArray values.

    is there a way to convert the JArray to a X++ list or array >?

  • Martin Dráb Profile Picture
    236,172 Most Valuable Professional on at
    RE: Deserialize JSON using C# dll file in x++

    Do you have any particular problem with JArray?

  • sylvesterPowerBi Profile Picture
    384 on at
    RE: Deserialize JSON using C# dll file in x++

    " iterate the JArray" that's what i was trying to achieve specially that i saw your reply here Iterate Jobject jarray in D365 but i didn't know to achieve it 

  • Martin Dráb Profile Picture
    236,172 Most Valuable Professional on at
    RE: Deserialize JSON using C# dll file in x++

    Yes, I know that you have a problem with FormJsonSerializer (but I don't know what kind of problem, therefore I can't comment on it). I didn't say that you should use FormJsonSerializer; I just showed how you would specify the type if you were using FormJsonSerializer. Sorry about confusing you by this extra detail.

    Note that there are many more options. If you aren't familiar with C# and you don't want to learn it, you could, for example, iterate the JArray and populate your contract class from it. Or maybe you could transform the JSON string to something acceptable by FormJsonSerializer (depending on what problem you have there).

  • sylvesterPowerBi Profile Picture
    384 on at
    RE: Deserialize JSON using C# dll file in x++

    Hi Martin, 

    as i mentioned above, using the FormJsonSerializer::deserializeObject(classId(MyContract), json);  is returning an error because of a value in the JSON.

    as for the second solution i tried the RetailCommonWebAPI::getMapFromJsonString(json) but i couldnt translate from the Map to a list/contract class.

    so the alternative was to use  a DLL in order to deserialize and insert the values in D365, i tried the Newtonsoft.Json.JsonConvert::DeserializeObject<Myclass>(wsResponse), but as you said generic is not supported in X++ so im not able to Build my solution tho.

    so i will appreciate any useful links, documentations for your solution (getting rid of X++) as i never used c# before.

  • Martin Dráb Profile Picture
    236,172 Most Valuable Professional on at
    RE: Deserialize JSON using C# dll file in x++

    The deserializer can't know that it should use your class if you doesn't give it this information. With FormJsonSerializer, you would do this:

    FormJsonSerializer::deserializeObject(classId(MyContract), json);

    If you use Newtonsoft.Json library, you could use the generic DeserializeObject<T>() method:

    JsonConvert.DeserializeObject();

    Because X doesn't support generic methods, you'll need to do it in C#. Using C# projects is easy, but you would have to add a reference to your model to be able to use the class in in C# project, and your model would refer to the C# project / assembly as well. You should break this circular dependency, which would make things more complex.

    If you can't do what you want in X , consider getting rid of the X contract class as well. You could take JSON, use Paste JSON as classes in Visual Studio to generate C# class to deserialize to and use this C# class in your X code (instead of the X contract class).

  • sylvesterPowerBi Profile Picture
    384 on at
    RE: Deserialize JSON using C# dll file in x++

    the problem with newtonsoft is that i need to fill the deserialized value from Newtonsoft.Json.JsonConvert::DeserializeObject into my contract class.

    the type retured is  Newtonsoft.Json.Linq.JArray

  • Maiden Profile Picture
    20 on at
    RE: Deserialize JSON using C# dll file in x++

    Hi sylvesterD365

    As far as I know there are at least 3 different methods to parse JSON strings. All of them are based on the NewtonSoft framework which can be used directly from X++. www.newtonsoft.com/.../Introduction.htm

    1. with Contract classes and FormJsonSerializer

    2. serial with the Newtonsoft.Json.JsonTextReader

    3. direct access with Newtonsoft.Json.Linq.JToken::Parse()

    regards

    Andi

  • sylvesterPowerBi Profile Picture
    384 on at
    RE: Deserialize JSON using C# dll file in x++

    Hi Will,

    i tried using the FormJsonSerializer::deserializeCollection method but it keeps returning an error, i opened a case with microsoft and they showed me where it returning the error,

    the point is to use another way to deserialize the JSON,

    can you advise how to do it in Newtonsoft.Json ?

    thank you

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

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

#1
CA Neeraj Kumar Profile Picture

CA Neeraj Kumar 2,352

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 634 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans