Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Suggested answer

JSON Deserialization returning null Contract class X++

(0) ShareShare
ReportReport
Posted on by 380

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

  • Martin Dráb Profile Picture
    Martin Dráb 230,414 Most Valuable Professional on at
    RE: JSON Deserialization returning null Contract class X++

    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.

  • sylvesterPowerBi Profile Picture
    sylvesterPowerBi 380 on at
    RE: JSON Deserialization returning null Contract class X++

    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
    Martin Dráb 230,414 Most Valuable Professional on at
    RE: JSON Deserialization returning null Contract class X++

    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
    sylvesterPowerBi 380 on at
    RE: JSON Deserialization returning null Contract class X++
    [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

  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 230,414 Most Valuable Professional on at
    RE: JSON Deserialization returning null Contract class X++

    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
    sylvesterPowerBi 380 on at
    RE: JSON Deserialization returning null Contract class X++

    Hi Ergun,

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

  • ergun sahin Profile Picture
    ergun sahin 8,812 Super User 2024 Season 1 on at

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

Congratulations 2024 Spotlight Honorees!

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December!

Congratulations to our December super stars! 🥳

Get Started Blogging in the Community

Hosted or syndicated blogging is available! ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,684 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,414 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans