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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

JSON Deserialization returning null Contract class X++

(0) ShareShare
ReportReport
Posted on by 384

Dears,

i am trying to deserialize JSON object in x using the below code

Myclass VarContract = FormJsonSerializer::deserializeObject(classNum(Myclass ),wsResponse);

myclass contract

[datacontract]
class Myclass
{
    list orders;
    [DataMember('orders'),AifCollectionTypeAttribute('orders',Types::Class,classStr(ElementClass))
        ,AifCollectionTypeAttribute('return',Types::Class,classStr(ElementClass))]
    public list ParmOrders(list _orders = orders)
    {
        orders = _orders;
        return orders;
    }

}

and the class Element is formated below

[datacontract]
class ElementClass
{
    [DataMember("id")]
    public int64 parmid(int64 _id = id)
    {
        id = _id;
        return id;
    }

       [DataMember("total_line_items_pric")]
    public Str Parmtotal_line_items_price(Str _total_line_items_price = total_line_items_price)
    {
        total_line_items_price = _total_line_items_price;
        return total_line_items_price;
    }

    [DataMember('total_discounts')]
    public Str total_discounts(Str _total_discounts = total_discounts)
    {
        total_discounts = _total_discounts;
        return total_discounts;
    }

    [DataMember('gateway')]
    public Str Parmgateway(Str _gateway = gateway)
    {
        gateway = _gateway;
        return gateway;
    }

    [DataMember('total_price')]
    public Str Parmtotal_price(Str _total_price = total_price)
    {
        total_price = _total_price;
        return total_price;
    }

    [DataMember('subtotal_price')]
    public Str Parmsubtotal_price(Str _subtotal_price = subtotal_price)
    {
        subtotal_price = _subtotal_price;
        return subtotal_price;
    }

}

after the deserialization the List in myclass is returning null knowing that the wsResponse is returning a well formated JSON 

your help is highly appreciated

  • ergun sahin Profile Picture
    8,826 Moderator on at
  • sylvesterPowerBi Profile Picture
    384 on at

    Hi Ergun,

    above links does not help me unfortunately, in my case i m referring to another class of type list

  • Suggested answer
    Martin Dráb Profile Picture
    240,852 Most Valuable Professional on at

    It's difficult to comment on it without knowing the data you're deserializing, nevertheless I do see a bug. You have a wrong value in AifCollectionTypeAttribute - the parameter is called _orders, not orders.

    By the way, please pay more attention to naming conventions such as letter cases. It'll make your code easier to read. For example,

    public Str Parmtotal_line_items_price(...)

    should be

    public str parmTotalLineItemsPrice(...)

  • sylvesterPowerBi Profile Picture
    384 on at
    [quote

    It's difficult to comment on it without knowing the data you're deserializing, nevertheless I do see a bug. You have a wrong value in AifCollectionTypeAttribute - the parameter is called _orders, not orders.

    By the way, please pay more attention to naming conventions such as letter cases. It'll make your code easier to read. For example,

    Fullscreen
    1
    public Str Parmtotal_line_items_price(...)
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    should be

    Fullscreen
    1
    public str parmTotalLineItemsPrice(...)
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    [/quote

    Hi Martin,

    please find below an example of the data getting deserialized

    "orders": 
        [
            {
                "id": 111111111,
                "email": "xxxxxx@gmail.com",
                "closed_at": "2021-02-22T18:57:20 02:00",
                "created_at": "2021-02-22T16:38:30 02:00",
                "updated_at": "2021-02-22T18:57:20 02:00",
                "number": 1234,
                "note": null
            },
             {
                "id": 222,
                "email": "xxxxxx@gmail.com",
                "closed_at": "2021-02-22T18:57:20 02:00",
                "created_at": "2021-02-22T16:38:30 02:00",
                "updated_at": "2021-02-22T18:57:20 02:00",
                "number": 231,
                "note": null
            }
        ]
            

    and thank you for your comment but replacing orders with _orders didn't resolve the issue

  • Martin Dráb Profile Picture
    240,852 Most Valuable Professional on at

    Hmm, how do you expect this to be deserialized? Your class doesn't match your data at all.

    Your class expects things like total_line_items_pric and total_discounts, which don't exist in your JSON string at all. Instead, you're getting values like closed_at that your class isn't designed to process.

  • sylvesterPowerBi Profile Picture
    384 on at

    i inserted a small specimen of the data to be deserialized, actually each sales order return around 300 line of JSON,

    the idea is, i am just giving an example of the json response

  • Martin Dráb Profile Picture
    240,852 Most Valuable Professional on at

    Then we can't do any review for you.

    Also, you have no idea what you did wrong because you're doing to many things at once. Do it differently. Keep just one or a few properties, verify that names matche exactly and try it again. By the way, don't forget the fix of AifCollectionTypeAttribute.

    If it works, you'll know that your approach is correct but you have a problem with some values that you excluded.

    If it fails, you'll be in a much better position to debug it. You won't be paralyzed anymore by the fact that you have 300 lines there.

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the August Top 10 Community Leaders

These are the community rock stars!

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

#1
Martin Dráb Profile Picture

Martin Dráb 523 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 408 Super User 2026 Season 2

#3
CU10121822-0 Profile Picture

CU10121822-0 354

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans