Announcements
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
I got the solution Thanks martin
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).
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.
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; } }
You can check the below link, might help to resolve the issue
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).
André Arnaud de Cal...
294,261
Super User 2025 Season 1
Martin Dráb
232,996
Most Valuable Professional
nmaenpaa
101,158
Moderator