Read the JSON data via REST API (X++)
Problem Statement:
Read the data from outside Application. Here I am fetch the JSON data from Shopify Integration and push it in to the D365FO System.
Development steps:
Step 1 : Create Runnable Class.
Step 2 : After create the Runnable class, use the following Library in your solution.
using System.Net.HttpWebRequest;
using System.Net.HttpWebResponse;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.IO.Stream;
using System.Exception;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization;
using System.Net.HttpWebResponse;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.IO.Stream;
using System.Exception;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization;
Step 3 : Write the code for connect shopify Application.
str destinationUrl = 'YourRESTAPIURL',responseJSON;
System.Net.HttpWebRequest request;
System.Net.HttpWebResponse response;
System.Net.WebHeaderCollection httpHeader;
System.IO.Stream requestStream,responseStream;
System.IO.StreamReader streamReader;
System.Exception ex;
CLRObject clrObj;
try new InteropPermission(InteropKind::ClrInterop).assert();
clrObj = System.Net.WebRequest::Create(destinationUrl);
httpHeader = new System.Net.WebHeaderCollection();
httpHeader.Add("Authorization","Basic YjFlY2UzMDE2YzZkZjM5NjJkN2I0O=");
request = clrObj;
request.set_KeepAlive(true);
request.set_ContentType("application/json");
request.set_Method("Get");
request.set_Headers(httpHeader);
response = request.GetResponse();
responseStream = response.GetResponseStream();
streamReader = new System.IO.StreamReader(responseStream);
responseJSON = streamReader.ReadToEnd().Trim();
//Get Sales Order from Shopify..
responseStream.Close();
KTI_ShopifyOrderIntegrationContract _shipifyContract = FormJsonSerializer::deserializeObject(classNum(KTI_ShopifyOrderIntegrationContract),responseJSON);
NOTE: Before writing the connection logic, pre-check with the POSTMAN application. Retrieve the encrypted authorization from POSTMAN
Step 4 : Define the Contract class based on the JSON Structure.
Example: ****Custom Collection Contract Class*****
[DataContract]
public class KTI_ShopifyOrderIntegrationContract extends FormDataContract
{
List orders;
[
DataMemberAttribute('orders'),
DataCollectionAttribute(Types::Class, classStr(orders))
]
public List parmorders(List _orders =orders)
{
orders = _orders;
return orders;
}
}
Example: ****Custom Contract Class*****
[DataContract]
public final class shop_money
{
Amount amount;
str currency_code;
str id;
[DataMember("amount")]
public Amount parmamount(Amount _amount = amount)
{
amount = _amount;
return amount;
}
[DataMember("currency_code")]
public str parmcurrency_code(str _currency_code = currency_code)
{
currency_code = _currency_code;
return currency_code;
}
[DataMemberAttribute("id")]
public str parmid(str _id = id)
{
id = _id;
return id;
}
}
Example: ****Custom Inner loop Contract Class*****
[DataContract]
Public final class line_items
{
List properties ;
int quantity ;
[
DataMemberAttribute('properties'),
DataCollectionAttribute(Types::Class, classStr(properties))
]
public List parmproperties( List _properties = properties)
{
if(_properties != null)
properties = _properties;
return properties;
}
[DataMember("quantity")]
public int parmquantity( int _quantity = quantity)
{
quantity = _quantity;
return quantity;
}
}
NOTE : JSON datatype and Name should be matched with the contact class attribute.
Comments
Post a Comment