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

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:

Blog161

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

Blog162

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:

Blog163.png

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:

Blog164

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:

Blog165

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

Blog166

That’s it! Well, almost. 😊 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:

Blog167

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:

Blog168.png

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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s