Hi alex0230,
You can use Newtonsoft.Json library.
Parse the JSON string to in Class object and then check with IF condition.
Please refer below sample.
Sample JSON
[
{
"id": 180,
"name": "Microsoft 365",
"type": {
"id": 29,
"name": "Cloud/Hosted Services",
"_info": { "type_href": "https: //sandbox-na.myconnectwise.net/v4_6_release/apis/3.0//finance/agreements/types/29" }
},
"company": {
"id": 19303,
"identifier": "AA",
"name": "Acai Associates",
"_info": { "company_href": "https://sandbox-na.myconnectwise.net/v4_6_release/apis/3.0//company/companies/19303" }
},
"contact": {
"id": 107,
"name": "Rafael Oranday",
"_info": { "contact_href": "https://sandbox-na.myconnectwise.net/v4_6_release/apis/3.0//company/contacts/107" }
},
"site": {
"id": 1095,
"name": "Main Office",
"_info": { "site_href": "https://sandbox-na.myconnectwise.net/v4_6_release/apis/3.0//company/companies/19303/sites/1095" }
},
"parentAgreement": {
"id": 1578,
"name": "Master Services Agreement",
"_info": { "agreement_href": "https://sandbox-na.myconnectwise.net/v4_6_release/apis/3.0//finance/agreements/1578" }
},
"location": {
"id": 11,
"name": "Lake Park",
"_info": { "location_href": "https://sandbox-na.myconnectwise.net/v4_6_release/apis/3.0//system/locations/11" }
},
"department": {
"id": 1,
"identifier": "Service",
"name": "Service",
"_info": { "department_href": "https://sandbox-na.myconnectwise.net/v4_6_release/apis/3.0//system/departments/1" }
},
"restrictLocationFlag": false,
"restrictDepartmentFlag": false,
"startDate": "2021-12-01T00:00:00Z",
"noEndingDateFlag": true,
"cancelledFlag": false,
"sla": {
"id": 2,
"name": "Standard SLA",
"_info": { "sla_href": "https://sandbox-na.myconnectwise.net/v4_6_release/apis/3.0//service/SLAs/2" }
},
"applicationUnits": "Hours",
"applicationLimit": 0.00,
"applicationCycle": "CalendarMonth",
"applicationUnlimitedFlag": true,
"oneTimeFlag": false,
"coverAgreementTime": true,
"coverAgreementProduct": false,
"coverAgreementExpense": false,
"coverSalesTax": false,
"carryOverUnused": false,
"allowOverruns": false,
"expireWhenZero": false,
"chargeToFirm": false,
"employeeCompRate": "Actual",
"employeeCompNotExceed": "Billing",
"compHourlyRate": 0.00,
"compLimitAmount": 0.00,
"billingCycle": {
"id": 2,
"name": "Monthly"
},
"billOneTimeFlag": false,
"billingTerms": {
"id": 1,
"name": "Net 30 days"
},
"invoicingCycle": "CalendarYear",
"billToCompany": {
"id": 19303,
"identifier": "AA",
"name": "Acai Associates",
"_info": { "company_href": "https://sandbox-na.myconnectwise.net/v4_6_release/apis/3.0//company/companies/19303" }
},
"billToContact": {
"id": 107,
"name": "Rafael Oranday",
"_info": { "contact_href": "https://sandbox-na.myconnectwise.net/v4_6_release/apis/3.0//company/contacts/107" }
},
"billToSite": {
"id": 1095,
"name": "Main Office",
"_info": { "site_href": "https://sandbox-na.myconnectwise.net/v4_6_release/apis/3.0//company/companies/19303/sites/1095" }
},
"billAmount": 0.00,
"taxable": false,
"billStartDate": "2022-03-01T00:00:00Z",
"taxCode": {
"id": 8,
"name": "AVATAX",
"_info": { "taxCode_href": "https://sandbox-na.myconnectwise.net/v4_6_release/apis/3.0//finance/taxCodes/8" }
},
"restrictDownPayment": true,
"prorateFlag": false,
"topComment": false,
"bottomComment": false,
"invoiceTemplate": {
"id": 10,
"name": "Monthly Agreement with Location Breakout",
"_info": { "billingTerms_Href": "https://sandbox-na.myconnectwise.net/v4_6_release/apis/3.0//finance/invoiceTemplateSetups/10" }
},
"billTime": "NoDefault",
"billExpenses": "NoDefault",
"billProducts": "NoDefault",
"billableTimeInvoice": false,
"billableExpenseInvoice": false,
"billableProductInvoice": false,
"currency": {
"id": 7,
"symbol": "$",
"currencyCode": "USD",
"decimalSeparator": ".",
"numberOfDecimals": 2,
"thousandsSeparator": ",",
"negativeParenthesesFlag": false,
"displaySymbolFlag": true,
"currencyIdentifier": "USD",
"displayIdFlag": false,
"rightAlign": false,
"name": "US Dollars",
"_info": { "currency_href": "https://sandbox-na.myconnectwise.net/v4_6_release/apis/3.0//finance/currencies/7" }
},
"autoInvoiceFlag": false,
"nextInvoiceDate": "2022-09-01T00:00:00Z",
"companyLocation": {
"id": 18,
"name": "Small and Medium Business",
"_info": { "location_href": "https://sandbox-na.myconnectwise.net/v4_6_release/apis/3.0//system/locations/18" }
},
"agreementStatus": "Active",
"_info": {
"lastUpdated": "2022-01-28T17:07:48Z",
"updatedBy": "WHough "
},
"customFields": [
{
"id": 32,
"caption": "MSA Start Date",
"type": "Date",
"entryMethod": "EntryField",
"numberOfDecimals": 0
},
{
"id": 33,
"caption": "MSA End Date",
"type": "Date",
"entryMethod": "EntryField",
"numberOfDecimals": 0
},
{
"id": 35,
"caption": "Contract Sales Person",
"type": "Text",
"entryMethod": "EntryField",
"numberOfDecimals": 0
}
]
}
]
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string json = System.IO.File.ReadAllText(Server.MapPath("~/Json.txt"));
List<Root> roots = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Root>>(json);
int i = 0;
foreach (Root root in roots)
{
if (root.parentAgreement != null)
{
ClientScript.RegisterStartupScript(this.GetType(), "alert" + i, "alert('Parent Agreement exists.');", true);
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "alert" + i, "alert('Parent Agreement not exists.');", true);
}
i++;
}
}
}
public class BillingCycle
{
public int id { get; set; }
public string name { get; set; }
}
public class BillingTerms
{
public int id { get; set; }
public string name { get; set; }
}
public class BillToCompany
{
public int id { get; set; }
public string identifier { get; set; }
public string name { get; set; }
public Info _info { get; set; }
}
public class BillToContact
{
public int id { get; set; }
public string name { get; set; }
public Info _info { get; set; }
}
public class BillToSite
{
public int id { get; set; }
public string name { get; set; }
public Info _info { get; set; }
}
public class Company
{
public int id { get; set; }
public string identifier { get; set; }
public string name { get; set; }
public Info _info { get; set; }
}
public class CompanyLocation
{
public int id { get; set; }
public string name { get; set; }
public Info _info { get; set; }
}
public class Contact
{
public int id { get; set; }
public string name { get; set; }
public Info _info { get; set; }
}
public class Currency
{
public int id { get; set; }
public string symbol { get; set; }
public string currencyCode { get; set; }
public string decimalSeparator { get; set; }
public int numberOfDecimals { get; set; }
public string thousandsSeparator { get; set; }
public bool negativeParenthesesFlag { get; set; }
public bool displaySymbolFlag { get; set; }
public string currencyIdentifier { get; set; }
public bool displayIdFlag { get; set; }
public bool rightAlign { get; set; }
public string name { get; set; }
public Info _info { get; set; }
}
public class CustomField
{
public int id { get; set; }
public string caption { get; set; }
public string type { get; set; }
public string entryMethod { get; set; }
public int numberOfDecimals { get; set; }
public object value { get; set; }
}
public class Department
{
public int id { get; set; }
public string identifier { get; set; }
public string name { get; set; }
public Info _info { get; set; }
}
public class Info
{
public string type_href { get; set; }
public string contact_href { get; set; }
public string site_href { get; set; }
public string taxCode_href { get; set; }
public string billingTerms_Href { get; set; }
public string currency_href { get; set; }
public string location_href { get; set; }
public DateTime lastUpdated { get; set; }
public string updatedBy { get; set; }
public string company_href { get; set; }
public string agreement_href { get; set; }
public string department_href { get; set; }
public string sla_href { get; set; }
}
public class InvoiceTemplate
{
public int id { get; set; }
public string name { get; set; }
public Info _info { get; set; }
}
public class Location
{
public int id { get; set; }
public string name { get; set; }
public Info _info { get; set; }
}
public class ParentAgreement
{
public int id { get; set; }
public string name { get; set; }
public Info _info { get; set; }
}
public class Root
{
public int id { get; set; }
public string name { get; set; }
public Type type { get; set; }
public Company company { get; set; }
public Contact contact { get; set; }
public Site site { get; set; }
public ParentAgreement parentAgreement { get; set; }
public Location location { get; set; }
public Department department { get; set; }
public bool restrictLocationFlag { get; set; }
public bool restrictDepartmentFlag { get; set; }
public DateTime startDate { get; set; }
public bool noEndingDateFlag { get; set; }
public bool cancelledFlag { get; set; }
public Sla sla { get; set; }
public string applicationUnits { get; set; }
public double applicationLimit { get; set; }
public string applicationCycle { get; set; }
public bool applicationUnlimitedFlag { get; set; }
public bool oneTimeFlag { get; set; }
public bool coverAgreementTime { get; set; }
public bool coverAgreementProduct { get; set; }
public bool coverAgreementExpense { get; set; }
public bool coverSalesTax { get; set; }
public bool carryOverUnused { get; set; }
public bool allowOverruns { get; set; }
public bool expireWhenZero { get; set; }
public bool chargeToFirm { get; set; }
public string employeeCompRate { get; set; }
public string employeeCompNotExceed { get; set; }
public double compHourlyRate { get; set; }
public double compLimitAmount { get; set; }
public BillingCycle billingCycle { get; set; }
public bool billOneTimeFlag { get; set; }
public BillingTerms billingTerms { get; set; }
public string invoicingCycle { get; set; }
public BillToCompany billToCompany { get; set; }
public BillToContact billToContact { get; set; }
public BillToSite billToSite { get; set; }
public double billAmount { get; set; }
public bool taxable { get; set; }
public DateTime billStartDate { get; set; }
public TaxCode taxCode { get; set; }
public bool restrictDownPayment { get; set; }
public bool prorateFlag { get; set; }
public bool topComment { get; set; }
public bool bottomComment { get; set; }
public InvoiceTemplate invoiceTemplate { get; set; }
public string billTime { get; set; }
public string billExpenses { get; set; }
public string billProducts { get; set; }
public bool billableTimeInvoice { get; set; }
public bool billableExpenseInvoice { get; set; }
public bool billableProductInvoice { get; set; }
public Currency currency { get; set; }
public bool autoInvoiceFlag { get; set; }
public DateTime nextInvoiceDate { get; set; }
public CompanyLocation companyLocation { get; set; }
public string agreementStatus { get; set; }
public Info _info { get; set; }
public List<CustomField> customFields { get; set; }
public string invoiceDescription { get; set; }
}
public class Site
{
public int id { get; set; }
public string name { get; set; }
public Info _info { get; set; }
}
public class Sla
{
public int id { get; set; }
public string name { get; set; }
public Info _info { get; set; }
}
public class TaxCode
{
public int id { get; set; }
public string name { get; set; }
public Info _info { get; set; }
}
public class Type
{
public int id { get; set; }
public string name { get; set; }
public Info _info { get; set; }
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim json As String = System.IO.File.ReadAllText(Server.MapPath("~/Json.txt"))
Dim roots As List(Of Root) = Newtonsoft.Json.JsonConvert.DeserializeObject(Of List(Of Root))(json)
Dim i As Integer = 0
For Each root As Root In roots
If root.parentAgreement IsNot Nothing Then
ClientScript.RegisterStartupScript(Me.GetType(), "alert" & i, "alert('Parent Agreement exists.');", True)
Else
ClientScript.RegisterStartupScript(Me.GetType(), "alert" & i, "alert('Parent Agreement not exists.');", True)
End If
i += 1
Next
End If
End Sub
Public Class BillingCycle
Public Property id As Integer
Public Property name As String
End Class
Public Class BillingTerms
Public Property id As Integer
Public Property name As String
End Class
Public Class BillToCompany
Public Property id As Integer
Public Property identifier As String
Public Property name As String
Public Property _info As Info
End Class
Public Class BillToContact
Public Property id As Integer
Public Property name As String
Public Property _info As Info
End Class
Public Class BillToSite
Public Property id As Integer
Public Property name As String
Public Property _info As Info
End Class
Public Class Company
Public Property id As Integer
Public Property identifier As String
Public Property name As String
Public Property _info As Info
End Class
Public Class CompanyLocation
Public Property id As Integer
Public Property name As String
Public Property _info As Info
End Class
Public Class Contact
Public Property id As Integer
Public Property name As String
Public Property _info As Info
End Class
Public Class Currency
Public Property id As Integer
Public Property symbol As String
Public Property currencyCode As String
Public Property decimalSeparator As String
Public Property numberOfDecimals As Integer
Public Property thousandsSeparator As String
Public Property negativeParenthesesFlag As Boolean
Public Property displaySymbolFlag As Boolean
Public Property currencyIdentifier As String
Public Property displayIdFlag As Boolean
Public Property rightAlign As Boolean
Public Property name As String
Public Property _info As Info
End Class
Public Class CustomField
Public Property id As Integer
Public Property caption As String
Public Property type As String
Public Property entryMethod As String
Public Property numberOfDecimals As Integer
Public Property value As Object
End Class
Public Class Department
Public Property id As Integer
Public Property identifier As String
Public Property name As String
Public Property _info As Info
End Class
Public Class Info
Public Property type_href As String
Public Property contact_href As String
Public Property site_href As String
Public Property taxCode_href As String
Public Property billingTerms_Href As String
Public Property currency_href As String
Public Property location_href As String
Public Property lastUpdated As DateTime
Public Property updatedBy As String
Public Property company_href As String
Public Property agreement_href As String
Public Property department_href As String
Public Property sla_href As String
End Class
Public Class InvoiceTemplate
Public Property id As Integer
Public Property name As String
Public Property _info As Info
End Class
Public Class Location
Public Property id As Integer
Public Property name As String
Public Property _info As Info
End Class
Public Class ParentAgreement
Public Property id As Integer
Public Property name As String
Public Property _info As Info
End Class
Public Class Root
Public Property id As Integer
Public Property name As String
Public Property type As Type
Public Property company As Company
Public Property contact As Contact
Public Property site As Site
Public Property parentAgreement As ParentAgreement
Public Property location As Location
Public Property department As Department
Public Property restrictLocationFlag As Boolean
Public Property restrictDepartmentFlag As Boolean
Public Property startDate As DateTime
Public Property noEndingDateFlag As Boolean
Public Property cancelledFlag As Boolean
Public Property sla As Sla
Public Property applicationUnits As String
Public Property applicationLimit As Double
Public Property applicationCycle As String
Public Property applicationUnlimitedFlag As Boolean
Public Property oneTimeFlag As Boolean
Public Property coverAgreementTime As Boolean
Public Property coverAgreementProduct As Boolean
Public Property coverAgreementExpense As Boolean
Public Property coverSalesTax As Boolean
Public Property carryOverUnused As Boolean
Public Property allowOverruns As Boolean
Public Property expireWhenZero As Boolean
Public Property chargeToFirm As Boolean
Public Property employeeCompRate As String
Public Property employeeCompNotExceed As String
Public Property compHourlyRate As Double
Public Property compLimitAmount As Double
Public Property billingCycle As BillingCycle
Public Property billOneTimeFlag As Boolean
Public Property billingTerms As BillingTerms
Public Property invoicingCycle As String
Public Property billToCompany As BillToCompany
Public Property billToContact As BillToContact
Public Property billToSite As BillToSite
Public Property billAmount As Double
Public Property taxable As Boolean
Public Property billStartDate As DateTime
Public Property taxCode As TaxCode
Public Property restrictDownPayment As Boolean
Public Property prorateFlag As Boolean
Public Property topComment As Boolean
Public Property bottomComment As Boolean
Public Property invoiceTemplate As InvoiceTemplate
Public Property billTime As String
Public Property billExpenses As String
Public Property billProducts As String
Public Property billableTimeInvoice As Boolean
Public Property billableExpenseInvoice As Boolean
Public Property billableProductInvoice As Boolean
Public Property currency As Currency
Public Property autoInvoiceFlag As Boolean
Public Property nextInvoiceDate As DateTime
Public Property companyLocation As CompanyLocation
Public Property agreementStatus As String
Public Property _info As Info
Public Property customFields As List(Of CustomField)
Public Property invoiceDescription As String
End Class
Public Class Site
Public Property id As Integer
Public Property name As String
Public Property _info As Info
End Class
Public Class Sla
Public Property id As Integer
Public Property name As String
Public Property _info As Info
End Class
Public Class TaxCode
Public Property id As Integer
Public Property name As String
Public Property _info As Info
End Class
Public Class Type
Public Property id As Integer
Public Property name As String
Public Property _info As Info
End Class
Screenshot