Sage 300: How to Create a Class in Visual Studio from JSON

2 minute read time.

Introduction

Some developers may already know how to create a class from JSON metadata. But, for those who do not, this little tip is very handy.

I’ll use the Sage 300 Web API and create classes based upon the ARCustomers endpoint. Of course, this also possible with any JSON metadata, but since this is a Sage 300 blog, using Sage 300 metadata feels right.

I give credit for this tip to Vega and I’m simply providing an illustrated example in this article. Thanks Vega!

Sage 300 Web API

The Sage 300 Web API is a great source for JSON metadata since the payloads are JSON. And, since the Web API has implemented the Swagger UI, it is even easier to access the metadata.

The first step is to access the Swagger UI. This is accomplished by navigating to your Sage 300 Web API and accessing the Open Swagger UI button:

After the Swagger UI has loaded the endpoints, scroll down to the ARCustomers endpoint and click on it to expand the available actions:

Click on the first GET action to expand the UI. In the Model Schema section, you will see the metadata for the ARCustomers payload. Copy this entire section:

Now, we have the JSON metadata in our buffer. It is time to move onto Visual Studio to create some C# classes based upon this metadata!

Visual Studio

In this example, I will be using Visual Studio 2017 and C#.

Open Visual Studio 2017 and Create a new class called ARCustomers:

Before we paste the contents of the buffer, delete the 3 lines for the ARCustomers class, which are indicated by the arrow in the image above:

We are now ready to paste our JSON. From the Edit menu, select Paste Special à Paste JSON As Classes:

That’s it! Well, almost. Blush As you can see, we now have C# classes that follow the hierarchy of the JSON metadata. It is very easy to see the hierarchy as well as all the column names:

The Paste Special feature names the top class Rootobject and the first or main class is named value. It makes more sense that they are named ARCustomers and ARCustomer respectively:

Tip

The arrays should be changed to IList especially when dealing with optional fields.

Summary

This article explained how to create C# classes in Visual Studio 2017 from JSON metadata that was copied from the Sage 300 Web API ARCustomers endpoint.

Any copied JSON metadata can use the Paste Special feature in Visual Studio to create classes based upon the JSON. I used the Sage 300 Web API as an example of metadata that will create several classes based upon the JSON.

Thanks again Vega for the tip!

As a standard disclaimer, any topic in this article is subject to review and doesn’t represent a commitment as to when it will be available.

  • This certainly gives you the structure, and for the most part, it gives you what you need but it is worth noting that the data types are not always correct. I see a lot of times where the data type is int and should be decimal. Most of the time I find that if it says float, you should change to decimal as well. In general, there is a little bit of trial and error here, but this really makes short work of getting the class structure right.