Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX forum
Unanswered

Parsing json getting error

Posted on by 25

This is the json Im trying to parse. I required 3 field only from this Json (valid, businessNumberType,exemptionReason.Name) but Im getting the error The type of object cannot be set.

{
"@recordsetCount": 1,
"value": [
{
"id": 1,
"companyId": 1,
"customerCode": "0102AGTUR001",
"name": "Test Customer",
"attnName": "Vern Jorgensen",
"line1": "26516 Dineral",
"city": "Mission Viejo",
"postalCode": "92691",
"phoneNumber": "555-555-5555",
"faxNumber": "444-444-4444",
"emailAddress": "someone@gmail.com",
"contactName": "Vern Jorgensen",
"createdDate": "2019-06-13T22:48:48",
"modifiedDate": "2023-03-02T22:23:25.799417",
"country": "US",
"region": "CA",
"isBill": false,
"isShip": false,
"certificates": [
{
"id": 909,
"signedDate": "2016-02-01",
"expirationDate": "2020-12-31",
"filename": "50afc677-8632-4a21-9fe1-45dafb1bb664",
"documentExists": false,
"valid": true,
"verified": false,
"exemptPercentage": 0.0,
"isSingleCertificate": false,
"exemptionNumber": "Exempt-1234",
"validatedExemptionReason": {
"id": 19,
"name": "EXPOSURE: EXPIRED CERT"
},
"exemptionReason": {
"id": 70,
"name": "RESALE"
},
"status": "COMPLETE",
"createdDate": "2021-03-24T06:42:00",
"modifiedDate": "2021-03-24",
"taxNumberType": "FEIN",
"businessNumberType": "Business Services",
"pageCount": 0,
"exposureZone": {
"id": 89,
"name": "Washington",
"tag": "EZ_US_WA"
}
}
]


}
]
}

Runnable class

Class Test
{

Public static void main(Args _args)
{
//call the APi

output = streamRead.ReadToEnd();

TestJSONDataContract TestJSONDataContract = FormJsonSerializer::deserializeObject(classNum(TestJSONDataContract),output);// IineCont getting the error
}
}

My Contract classes

//TestJSONDataContract only one contract method
[DataContract]
Class TestJSONDataContract
{
[DataMemberAttribute('value'),DataCollectionAttribute(Types::class, classStr(JsonObjectDataContract))]
public List parmValue(List _value = value)
{
value = _value;
return value;
}
}

[DataContract]
Class JsonObjectDataContract
{
[DataMemberAttribute("certificates"),
DataCollectionAttribute(Types::Class, classStr(CustomerCertContract))]
public List parmCertificates(List _certificates = certificates)
{
certificates = _certificates;
return certificates;
}
}

[DataContract]
Class CustomerCertContract
{
[DataMember("valid")]
public boolean parmValid(boolean _valid = valid)
{
valid = _valid;
return valid;
}

[DataMember("businessNumberType")]
public str parmBusinessNumberType(str _businessNumberType = businessNumberType)
{
businessNumberType = _businessNumberType;
return businessNumberType;
}
}

Please help me where I am going wrong

  • Vishnu@94 Profile Picture
    Vishnu@94 25 on at
    RE: Parsing json getting error

    I got the solution Thanks martin

  • Martin Dráb Profile Picture
    Martin Dráb 225,490 Super User on at
    RE: Parsing json getting error

    Your code still lacks line indentation. It should look like this:

    {
      "@recordsetCount": 1,
      "value": [
        {
          "id": 1,
          "companyId": 1,
          "customerCode": "0102AGTUR001",
          "name": "Test Customer",
          "attnName": "Vern Jorgensen",
          "line1": "26516 Dineral",
          "city": "Mission Viejo",
          "postalCode": "92691",
          "phoneNumber": "555-555-5555",
          "faxNumber": "444-444-4444",
          "emailAddress": "someone@gmail.com",
          "contactName": "Vern Jorgensen",
          "createdDate": "2019-06-13T22:48:48",
          "modifiedDate": "2023-03-02T22:23:25.799417",
          "country": "US",
          "region": "CA",
          "isBill": false,
          "isShip": false,
          "certificates": [
            {
              "id": 909,
              "signedDate": "2016-02-01",
              "expirationDate": "2020-12-31",
              "filename": "50afc677-8632-4a21-9fe1-45dafb1bb664",
              "documentExists": false,
              "valid": true,
              "verified": false,
              "exemptPercentage": 0,
              "isSingleCertificate": false,
              "exemptionNumber": "Exempt-1234",
              "validatedExemptionReason": {
                "id": 19,
                "name": "EXPOSURE: EXPIRED CERT"
              },
              "exemptionReason": {
                "id": 70,
                "name": "RESALE"
              },
              "status": "COMPLETE",
              "createdDate": "2021-03-24T06:42:00",
              "modifiedDate": "2021-03-24",
              "taxNumberType": "FEIN",
              "businessNumberType": "Business Services",
              "pageCount": 0,
              "exposureZone": {
                "id": 89,
                "name": "Washington",
                "tag": "EZ_US_WA"
              }
            }
          ]
        }
      ]
    }

    (I used json-indent.com to fix indentation.)

    class Test
    {
    	public static void main(Args _args)
    	{
    		//call the APi
    
    		output = streamRead.ReadToEnd();
    
    		TestJSONDataContract testJSONDataContract = FormJsonSerializer::deserializeObject(classNum(TestJSONDataContract), output);// IineCont getting the error
    	}
    }
    
    //Contract class
    [DataContract]
    class TestJSONDataContract
    {
    	[	DataMemberAttribute('value'),
    		DataCollectionAttribute(Types::class, classStr(JsonObjectDataContract))]
    	public List parmValue(List _value = value)
    	{
    		value = _value;
    		return value;
    	}
    }
    
    [DataContract]
    class JsonObjectDataContract
    {
    	[	DataMemberAttribute("certificates"),
    		DataCollectionAttribute(Types::Class, classStr(CustomerCertContract))]
    	public List parmCertificates(List _certificates = certificates)
    	{
    		certificates = _certificates;
    		return certificates;	
    	}
    }
    
    class CustomerCertContract
    {
    	[DataMember("valid")]
    	public boolean parmValid(boolean _valid = valid)
    	{
    		valid = _valid;
    		return valid;
    	}
    
    	[DataMember("businessNumberType")]
    	public str parmBusinessNumberType(str _businessNumberType = businessNumberType)
    	{
    		businessNumberType = _businessNumberType;
    		return businessNumberType;
    	}
    }

    Won't you get more details about the problem if you debug FormJsonSerializer?

    Please tell us which version of AX you're using. It seems that it's either AX 2012 (and we need to update the version tag), or it's actually about F&O (and I'll move it to Dynamics 365 Finance Forum).

  • Vishnu@94 Profile Picture
    Vishnu@94 25 on at
    RE: Parsing json getting error

    MuthukumaranAX  I tried the above way but still Im getting the error.

  • Vishnu@94 Profile Picture
    Vishnu@94 25 on at
    RE: Parsing json getting error

    Hi Martin I have formatted the code now Could you please have a look into that. Is it required to put all the fields in the JSON to the contract class.

  • Vishnu@94 Profile Picture
    Vishnu@94 25 on at
    RE: Parsing json getting error

    Class Test
    {
    
    public static void main(Args _args)
    {
    //call the APi
    
    output = streamRead.ReadToEnd();
    
    TestJSONDataContract TestJSONDataContract = FormJsonSerializer::deserializeObject(classNum(TestJSONDataContract),output);// IineCont getting the error
    }
    }
    
    //Contract class
    [DataContract]
    Class TestJSONDataContract
    {
    [DataMemberAttribute('value'),DataCollectionAttribute(Types::class, classStr(JsonObjectDataContract))]
    public List parmValue(List _value = value)
    {
    value = _value;
    return value;
    }
    }

    Class CustomerCertContract
    {
    [DataMember("valid")]
    public boolean parmValid(boolean _valid = valid)
    {
    valid = _valid;
    return valid;
    }
    
    [DataMember("businessNumberType")]
    public str parmBusinessNumberType(str _businessNumberType = businessNumberType)
    {
    businessNumberType = _businessNumberType;
    return businessNumberType;
    }
    }

  • MuthukumaranAX Profile Picture
    MuthukumaranAX 2,897 on at
    RE: Parsing json getting error

    You can check the below link, might help to resolve the issue

    allaboutdynamic.com/.../

  • Martin Dráb Profile Picture
    Martin Dráb 225,490 Super User on at
    RE: Parsing json getting error

    Could you please paste code with indentation (through Insert > Code) and mention your version of AX?

    Line indentation makes code much easier to follow. You can even enable syntax highlighting for JSON (not X++, unfortunately).

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,696 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,490 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans