Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Convert Json to X++ and How to deserialize it?

(0) ShareShare
ReportReport
Posted on by 85

Hello 

Can you please help me understand, how I can write code in F&O (X++) to convert below C# code (JSON) to X++ and deserialize it. Also how I can loop through all the multiple records in Json to assign/populate value from json record to AX table field.?

I know we can write a contract class, but Im confused whether to write multiple contract class (as we wrote in C# ?)

public class Project
{
public int id { get; set; }
public string name { get; set; }
}
public class Entry
{
public double hours { get; set; }
public string notes { get; set; }
public Project project { get; set; }
public object invoice { get; set; }
}

public class Root
{
public List<Entry> entries { get; set; }
}

  • Verified answer
    Martin Dráb Profile Picture
    232,854 Most Valuable Professional on at
    RE: Convert Json to X++ and How to deserialize it?

    When you deserialize the string, you'll get objects. You won't look through anything in the JSON string; you'll loop through elements of the List object.

    For example:

    MyLibrary.Root root = MyLibrary.MyClass::GetData();
    System.IEnumerator entryEnumerator = root.entries.GetEnumerator();
    while (entryEnumerator.MoveNext)
    {
    	MyLibrary.Entry entry = entryEnumerator.Current;
    }

    By the way, your code doesn't respect C# naming conventions. Property names (such as entries) should start with a capital letter.

    Note that I thought you already have C# and you know how to use, that's why I suggested you could use it directly instead of rewriting it to X . If it's not the case, using X will probably be better for you.

  • RahulD365 Profile Picture
    85 on at
    RE: Convert Json to X++ and How to deserialize it?

    I meant to say - How I can retrieve the values of the fields when we deserialize the string(response) and how can I loop through each elements in the Json?

  • Martin Dráb Profile Picture
    232,854 Most Valuable Professional on at
    RE: Convert Json to X++ and How to deserialize it?

    I'm a bit confused. What do you mean by "to convert it in C#"? You used "to convert" when talking about rewriting C# to X++, but "converting it in C#" doesn't make sense in such a context. Do you mean how to deserialize JSON files in C#?

  • RahulD365 Profile Picture
    85 on at
    RE: Convert Json to X++ and How to deserialize it?

    Thanks Sergei! Appreciate it!

  • RahulD365 Profile Picture
    85 on at
    RE: Convert Json to X++ and How to deserialize it?

    Thanks Martin, Can you please help me with any sample code? Like I want to learn, how we can convert it in C# and loop/iterate to get the data?

  • Suggested answer
    Martin Dráb Profile Picture
    232,854 Most Valuable Professional on at
    RE: Convert Json to X++ and How to deserialize it?

    Another approach is not converting it to X at all - you can call C# libraries from X , therefore your C# code could deserialize the file and simple return a Root object with the data, which you'll use in X for whatever you need.

    By the way, please use Insert > Insert Code (in the rich formatting view) to paste source code. Look at the difference:

    public class Project
    {
    	public int id { get; set; }
    	public string name { get; set; }
    }
    
    public class Entry
    {
    	public double hours { get; set; }
    	public string notes { get; set; }
    	public Project project { get; set; }
    	public object invoice { get; set; }
    }
    
    public class Root
    {
    	public List entries { get; set; }
    }

  • Verified answer
    Sergei Minozhenko Profile Picture
    23,091 on at
    RE: Convert Json to X++ and How to deserialize it?

    Hi RahulD365,

    You can take a look at method FormJsonSerializer::deserializeCollection and use cross-references to check how it's used for list object deserialization.

    Steps for conversions

    1. Create data contract classes in x++ for Project and Entity (docs.microsoft.com/.../walkthrough-exposing-an-x-class-as-a-data-contract )

    2. Use FormJsonSerializer::deserializeCollection to deserialize json to list of Entry objects

    The only field which can cause issues is Entry.invoice because of object type. Do you know what is stored in that field?

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,033 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,854 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Product updates

Dynamics 365 release plans