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

JSON serialization of a class that contains a list/array of objects of another class in X++

(0) ShareShare
ReportReport
Posted on by 30

As the subject suggests, I am trying to do the JSON serialization of an object, an instance of a class that, among other properties, has a list or array (I don't know what's better, that could be another question) of objects, instances of another class. Basically, my main class is of a header-details type. When I try JSON serialization using Newtonsoft.Json.JsonConvert::SerializeObject method, the main object is serialized correctly, but its Details property is empty in the JSON. What am I missing or doing wrong?

I have the same question (0)
  • Bharani Preetham Peraka Profile Picture
    3,634 Moderator on at

    Empty means either your details list is null completely. Means you did not implement the details class. Can you send the parm method for this class?

  • Suggested answer
    Anton Venter Profile Picture
    20,345 Super User 2025 Season 2 on at

    To serialize correctly as children of the parent, the child objects must be contained in a class member variable like a List for example and be exposed properly (method must be decorated as such). Below is an untested example just to demonstrate what I mean. I use the FormJsonSerializer class to serialize/deserialize but the NewtonSoft library should work as well. If you search online for FormJsonSerializer, you will find some alternative examples.

    public class MyHeaderClass
    {
    	protected List childrenList;
    
        [DataMemberAttribute('Lines'), DataCollectionAttribute(Types::Class, classStr(MyLineClass))]
        public List parmChildren(List _childrenList = childrenList)
        {
            childrenList = _childrenList;
            return childrenList;
        }
    }

  • Ascalab Profile Picture
    30 on at

    I have implemented it the way you suggested, but the output was still empty details list. I eventually found a solution that looks a bit silly, but it worked. I found it on another thread of this forum, and it was posted by the user M.K. It's not marked as a verified answer, but it worked for me (and for another member of the mentioned discussion). The command that worked is:

    str formatedJSON = JsonConvert::SerializeObject(JsonConvert::DeserializeObject(FormJsonSerializer::serializeClass(object)), Formatting::Indented);

    So the object was first serialized using FormJsonSerializer, and then deserialized and serialized again using Newtonsoft's JsonConvert class.

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