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

changing contract class

(1) ShareShare
ReportReport
Posted on by 2,025
Hi,

I have a custom service that accepts this contract class as an input
{
    "_input": 
    {
        "header": 
        {
            "dataArea": "A1",
            "mainId": "1",
            "line": 
            [
                {
                   
                    "fieldA": 1,
                    "id": "Id1"
                },
		{
                   
                    "fieldA": 2,
                    "id": "Id2"
                },
                {
                   
                    "fieldA": 3,
                    "id": "Id1"
                }
            ] 
        }
    }
}

After accepting this contract as an input to the custom service, how can i add x++ code to convert it to the format below? ( i want to group the ids in lines)
{
    "_input": 
    {
        "header": 
        {
            "dataArea": "A1",
            "mainId": "1",
            "ids": 
            [
                {
                    "id": "id1",
                    "Line":
                    [
                        {
                            "fieldA": 1
                        },
                        {
                            "fieldA": 3
                        }
                    ]
                },
                {
                    "id": "id2",
                    "line":
                    [
                        {
                            "fieldA": 2
                        }
                    ]
                }
            ]
        }
    }
}
 
  • Suggested answer
    Martin Dráb Profile Picture
    241,637 Most Valuable Professional on at
    These are JSON strings, not contract classes. You'll define a contract class for the input JSON, use it as a parameter of your custom service and what you'll receive is an instance of your contract class populated with the data from the JSON string. You'll not receive the JSON string and therefore you don't need to worry at all about how to parse it or convert it in X++. If you want take data from your contract object and put it to another object, it's just normal object-oriented programming.
  • .. Profile Picture
    2,025 on at
    Hi Martin,
     
    Yes I mean the contract class i have represents this Json.
     
    However, i want to re-arrange the received contract to the 2nd format. How can i do that? How can i group the first contract by Ids to achieve the 2nd format? Do i need to use maps for example an what to do exactly?
  • Martin Dráb Profile Picture
    241,637 Most Valuable Professional on at
    My point is that the code you showed is irrelevant to your question. Forget JSON and focus on objects in X++. Please show us your contract classes instead and explain where you got stuck when trying to write your X++.
     
    The first thing you'll need is iterating line contract objects. Do you know how to iterate elements of a list or do you need our help with that too?

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

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

#1
André Arnaud de Calavon Profile Picture

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

#2
Aayush Tiwari Profile Picture

Aayush Tiwari 217

#3
Subra Profile Picture

Subra 200 Super User 2026 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans