Failed to parse JSON request content

Hi Guys,

I'm using the below code to Post JSON request to a REST API, but I'm getting "Failed to parse JSON request content." Error,

Set Body = ##class(%ZEN.proxyObject).%New()
 Set Body.ElectronicMailAddressT = "ElectronicMailAddressT"
 Set Body.TelephoneMinimalN = "TelephoneMinimalN"
 Set Body.AustralianBusinessNumberId = "AustralianBusinessNumberId"
 Set Body.OrganisationDetailsOrganisationBranchC = "OrganisationDetailsOrganisationBranchC"
 Set Body.DetailsOrganisationalNameT = "DetailsOrganisationalNameT"
 Set Body.PersonUnstructuredNameFullNameT = "PersonUnstructuredNameFullNameT"
 Set Body.Line1T = "Line1T"
 Set Body.Line2T = "Line2T"
 Set Body.LocalityNameT = "LocalityNameT"
 Set Body.PostcodeT = "PostcodeT"
 Set Body.CountryC = "CountryC"
 Set Body.PayAsYouGoWithholdingTaxWithheldA = 0.1
 Set Body.TotalGrossPaymentsWithholdingA = 0.1
 Set Body.PaymentRecordTransactionD = "PaymentRecordTransactionD"
 Set Body1 = ##class(%ZEN.proxyObject).%New()
 Set Body1.IndividualNonBusinessEmploymentAllowancesA = 0.1
 Set Body1.AllowanceTypeTypeC = "AllowanceTypeTypeC"
 Set Body1.OtherAllowanceTypeDe = "OtherAllowanceTypeDe"
 Set Body1.DeductionTypeTypeC = "DeductionTypeTypeC"
 Set Body1.DeductionTypeC = 0.1
 Set Body1.EmploymentPayrollNumberId = "EmploymentPayrollNumberId"
 Set Body1.TaxFileNumberId = "TaxFileNumberId"
 Set Body1.FamilyNameT = "FamilyNameT"
 Set Body1.GivenNameT = "GivenNameT"
 Set Body1.OtherGivenNameT = "OtherGivenNameT"
 Set Body1.BirthDate = "2014-01-11"
 Set Body1.Line1T = "Line1T"
 Set Body1.Line2T = "Line2T"
 Set Body1.LocalityNameT = "LocalityNameT"
 Set Body1.StateOrTerritoryC = "StateOrTerritoryC"
 Set Body1.PostcodeT = "PostcodeT"
 Set Body1.CountryC = "CountryC"
 Set Body1.EmploymentStartD = "2014-01-11"
 Set Body1.EmploymentEndD = "2014-01-11"
 Set Body1.ElectronicMailAddressT = "ElectronicMailAddressT"
 Set Body1.TelephoneMinimalN = "TelephoneMinimalN"
 Set Body1.EmployerReportableA = 0.1
 Set Body1.OrdinaryTimeEarningsA = 0.1
 Set Body1.EmployerContributionsSuperannuationGuaranteeA = 0.1
 Set Body1.LabourHireTaxWithheldA = 0.1
 Set Body1.ExemptIncomeFringeBenefitsReportableA = 0.1
 Set Body1.TaxableIncomeFringeBenefitsReportableA = 0.1
 Set Body1.INBTaxWithheldA = 0.1
 Set Body1.INBGrossA = 0.1
 Set Body1.ExemptForeignEmploymentIncomeA = 0.1
 Set Body1.CommunityDevelopmentEmploymentProjectA = 0.1
 Set Body1.LumpSumBA = 0.1
 Set Body1.LumpSumDA = 0.1
 Set Body1.LumpSumEA = 0.1
 Set Body1.WorkingHolidayGrossA = 0.1
 Set Body1.WorkingHolidayTaxWithheldA = 0.1
 Set Body1.LumpSumAA = 0.1
 Set Body1.LumpSumAC = 0.1
 Set Body1.WorkingHolidayGrossA = 0.1
 Set Body2 = ##class(%ZEN.proxyObject).%New()
 Set Body2.TaxWithheldA = 0.1
 Set Body2.SuperannuationTaxFreeComponentA = 0.1
 Set Body2.SuperannuationEmploymentTerminationTaxableComponentTotalA = 0.1
 Set Body2.PaymentRecordPaymentEffectiveD = "2014-01-11"
 Set Body2.EmploymentTerminationPaymentTypeC = "EmploymentTerminationPaymentTypeC"
 Set TerminationPaymentSummary = ##class(%ListOfDataTypes).%New()
 Do TerminationPaymentSummary.Insert(Body2)
 Set Body1.TerminationPaymentSummary = TerminationPaymentSummary
 Set Payee = ##class(%ListOfDataTypes).%New()
 Do Payee.Insert(Body1)
 Set Body.Payee = Payee
 Set Request= ##class(%Net.HttpRequest).%New()
 //D Body.%ToJSON()
 Set Request.Server = "server"
 Set Request.Location = "location"
 Set Request.ContentType = "application/json"
 D Request.SetHeader("Source","Civicview")
 D Request.SetHeader("PayrollStartDate","2017-01-11")
 D Request.SetHeader("PayrollEndDate","2017-02-12")
 Set Request.Https=1
 Set Request.SSLConfiguration="TLS"
 Set Status = ##class(%ZEN.Auxiliary.jsonProvider).%WriteJSONStreamFromObject(Request.EntityBody, Body)
 Set Status = Request.Post(,2)
 I Status W !, "Request Sent"

My code is generating this JSON request:

{
        "AustralianBusinessNumberId":"AustralianBusinessNumberId",
        "CountryC":"CountryC",
        "DetailsOrganisationalNameT":"DetailsOrganisationalNameT",
        "ElectronicMailAddressT":"ElectronicMailAddressT",
        "Line1T":"Line1T",
        "Line2T":"Line2T",
        "LocalityNameT":"LocalityNameT",
        "OrganisationDetailsOrganisationBranchC":"OrganisationDetailsOrganisationBranchC",                                                                              "PayAsYouGoWithholdingTaxWithheldA":0.1,
        "Payee":[ {
                        "AllowanceTypeTypeC":"AllowanceTypeTypeC",
                        "BirthDate":"2014-01-11",
                        "CommunityDevelopmentEmploymentProjectA":0.1,
                        "CountryC":"CountryC",
                        "DeductionTypeC":0.1,
                        "DeductionTypeTypeC":"DeductionTypeTypeC",
                        "ElectronicMailAddressT":"ElectronicMailAddressT",
                        "EmployerContributionsSuperannuationGuaranteeA":0.1,
                        "EmployerReportableA":0.1,
                        "EmploymentEndD":"2014-01-11",
                        "EmploymentPayrollNumberId":"EmploymentPayrollNumberId",
                        "EmploymentStartD":"2014-01-11",
                        "ExemptForeignEmploymentIncomeA":0.1,
                        "ExemptIncomeFringeBenefitsReportableA":0.1,
                        "FamilyNameT":"FamilyNameT",
                        "GivenNameT":"GivenNameT",
                        "INBGrossA":0.1,
                        "INBTaxWithheldA":0.1,
                        "IndividualNonBusinessEmploymentAllowancesA":0.1,
                        "LabourHireTaxWithheldA":0.1,
                        "Line1T":"Line1T",
                        "Line2T":"Line2T",
                        "LocalityNameT":"LocalityNameT",
                        "LumpSumAA":0.1,
                        "LumpSumAC":0.1,
                        "LumpSumBA":0.1,
                        "LumpSumDA":0.1,
                        "LumpSumEA":0.1,
                        "OrdinaryTimeEarningsA":0.1,
                        "OtherAllowanceTypeDe":"OtherAllowanceTypeDe",
                        "OtherGivenNameT":"OtherGivenNameT",
                        "PostcodeT":"PostcodeT",
                        "StateOrTerritoryC":"StateOrTerritoryC",
                        "TaxFileNumberId":"TaxFileNumberId",
                        "TaxableIncomeFringeBenefitsReportableA":0.1,
                        "TelephoneMinimalN":"TelephoneMinimalN",
                        "TerminationPaymentSummary":[ {
                                        "EmploymentTerminationPaymentTypeC":"EmploymentTerminationPaymentTypeC",
                                        "PaymentRecordPaymentEffectiveD":"2014-01-11",
                                        "SuperannuationEmploymentTerminationTaxableComponentTotalA":0.1,
                                        "SuperannuationTaxFreeComponentA":0.1,
                                        "TaxWithheldA":0.1
                                }
                        ],
                        "WorkingHolidayGrossA":0.1,
                        "WorkingHolidayTaxWithheldA":0.1
                }
        ],
        "PaymentRecordTransactionD":"PaymentRecordTransactionD",
        "PersonUnstructuredNameFullNameT":"PersonUnstructuredNameFullNameT",
        "PostcodeT":"PostcodeT",
        "TelephoneMinimalN":"TelephoneMinimalN",
        "TotalGrossPaymentsWithholdingA":0.1
}

But the REST API is expecting this JSON request:

{
  "ElectronicMailAddressT": "string",
  "TelephoneMinimalN": "string",
  "AustralianBusinessNumberId": "string",
  "OrganisationDetailsOrganisationBranchC": "string",
  "DetailsOrganisationalNameT": "string",
  "PersonUnstructuredNameFullNameT": "string",
  "Line1T": "string",
  "Line2T": "string",
  "LocalityNameT": "string",
  "StateOrTerritoryC": "string",
  "PostcodeT": "string",
  "CountryC": "string",
  "PayAsYouGoWithholdingTaxWithheldA": 0.1,
  "TotalGrossPaymentsWithholdingA": 0.1,
  "PaymentRecordTransactionD": "2014-12-31",
  "Payee": [
    {
      "IndividualNonBusinessEmploymentAllowancesA": 0.1,
      "AllowanceTypeTypeC": "string",
      "OtherAllowanceTypeDe": "string",
      "DeductionTypeTypeC": "string",
      "DeductionTypeC": 0.1,
      "EmploymentPayrollNumberId": "string",
      "TaxFileNumberId": "string",
      "FamilyNameT": "string",
      "GivenNameT": "string",
      "OtherGivenNameT": "string",
      "BirthDate": "2014-12-31",
      "Line1T": "string",
      "Line2T": "string",
      "LocalityNameT": "string",
      "StateOrTerritoryC": "string",
      "PostcodeT": "string",
      "CountryC": "string",
      "EmploymentStartD": "2014-12-31",
      "EmploymentEndD": "2014-12-31",
      "ElectronicMailAddressT": "string",
      "TelephoneMinimalN": "string",
      "EmployerReportableA": 0.1,
      "OrdinaryTimeEarningsA": 0.1,
      "EmployerContributionsSuperannuationGuaranteeA": 0.1,
      "LabourHireTaxWithheldA": 0.1,
      "ExemptIncomeFringeBenefitsReportableA": 0.1,
      "TaxableIncomeFringeBenefitsReportableA": 0.1,
      "INBTaxWithheldA": 0.1,
      "INBGrossA": 0.1,
      "ExemptForeignEmploymentIncomeA": 0.1,
      "CommunityDevelopmentEmploymentProjectA": 0.1,
      "LumpSumBA": 0.1,
      "LumpSumDA": 0.1,
      "LumpSumEA": 0.1,
      "WorkingHolidayGrossA": 0.1,
      "WorkingHolidayTaxWithheldA": 0.1,
      "LumpSumAA": 0.1,
      "LumpSumAC": "string",
      "TerminationPaymentSummary": [
        {
          "TaxWithheldA": 0.1,
          "SuperannuationTaxFreeComponentA": 0.1,
          "SuperannuationEmploymentTerminationTaxableComponentTotalA": 0.1,
          "PaymentRecordPaymentEffectiveD": "2014-12-31",
          "EmploymentTerminationPaymentTypeC": "string"
        }
      ]
    }
  ]
}

I validated both JSON requests and they validate OK  

The only difference I can see is the Comma after the array "]," in my request, so i'm not sure if that's the reason and hwo to fix it?

 

Thanks Guys and sorry for the long message but I wanted to give you the full picture.

  • 0
  • 0
  • 1147
  • 5
  • 2

Answers

The error says parse error, but in this case I think it means either datatype validation (probably)  or fixed property order (less probable, but that can actually happen). My default advice in cases like this is as follows:

  1. Set up a way to send valid requests
  2. Send request via Caché/Ensemble
  3. Compare (DiffDog, etc) requests from 1 and 2, here you need to:
    • Modify valid request till it fails
    • Modify invalid request  till it succeeds

In your case the first modification I would do is replacing dates with real dates, for example with 2014-01-11.

Sorry I'm lost here. and not sure what you in 1,2 or 3 !?

The above request that I created in Cache was working fine and Posts ok to the required REST API, but then new requirement came up and added two array to it the REST API, and they send me the new JSON structure so I had to do the same to accommodate the change (the JSOM after But the REST API is expecting this JSON request:)!?

the other difference is that Now they also require a Header, before they they only had a Body in their API.  

At first I thought that could be the comma after "]" in my JSON, but looks like is not the case, and I'm also wondering the way I added the header is correct or not? or do I also need to define the Header type to JSON as I did with content (Set Request.ContentType "application/json")?

In your case the first modification I would do is replacing dates with real dates, for example with 2014-01-11.

I think my date is already like that!? do you mean removing the double quotes or changing it to YYYY-DD-MM?

 

 

thanks Eduard 

 

 

Sorry I'm lost here. and not sure what you in 1,2 or 3 !?

It's an iterative process to pinpoint and remove errors. You start by establishing a channel which can send valid requests. For example using Postman. Next you compare valid request (which you can now consistently send) with your request which does not pass. Next you iteratively break valid request or fix failed request or send failed request via Postman. Sooner or later you understand what causes your request to fail and fix that.

the other difference is that Now they also require a Header, before they they only had a Body in their API.  

You need to add all required headers via SetHeader method.

I think my date is already like that!? do you mean removing the double quotes or changing it to YYYY-DD-MM?

You're sending:

"PaymentRecordTransactionD":"PaymentRecordTransactionD"

But API expects:

PaymentRecordTransactionD": "2014-12-31"

Looks different for me.

Thanks by changing the dates it all worked out ok now.

Thanks again

I think the problem is, that REST server is not so clear in error information. And maybe it just expects something else in your data. For example "PaymentRecordTransactionD" expects to get some Date, but you send string.

Some fields may also have max length check.

Thanks Dmitry, I'll confirm the specs will API provider.