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

Announcements

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,591 Super User 2026 Season 1 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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

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

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 666 Super User 2026 Season 1

#2
Abhilash Warrier Profile Picture

Abhilash Warrier 280 Super User 2026 Season 1

#3
Martin Dráb Profile Picture

Martin Dráb 207 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans