Home > OS >  XERO API - JSON for post data was invalid,Cannot deserialize the current JSON object
XERO API - JSON for post data was invalid,Cannot deserialize the current JSON object

Time:03-29

I am using the Xero API, and following the docs but having a problem sending some specific data to this end point: https://developer.xero.com/documentation/api/accounting/invoices#post-invoices

Here is my array in PHP:

$customer_details = array(
    'Name' => CompanyName($result["customer"], 'company'),
    'AccountNumber' => '',
    'EmailAddress' => '',
    'Addresses' => (array(
        'AddressType' => 'POBOX',
        //'AddressLine 1,2,3,4' => array($address["line1"], $address["line2"], $address["line3"]),
        'City' => $address["town"],
        'Region' => $address["county"],
        'PostalCode' => $address["postcode"],
    )),
);

$invoice_line_items = array();
$sql2 = $mysqli->query("SELECT * FROM invoices_export WHERE export = '' GROUP BY customer;");
while($result2 = $sql2->fetch_array()) {
    $invoice_line_items[]= array(
        'Description' => $result["description"],
        'Quantity' => $result["quantity"],
        'UnitAmount' => $result["unit_price"],
        'AccountCode' => '200',
    );
}

$invoice_data = array(
    'Type' => 'ACCREC',
    'Status' => 'DRAFT',
    'Contact' => $customer_details,
    'LineItems' => $invoice_line_items,
);

But the error I receive is this:

string(362) "{"Type":"ACCREC","Status":"DRAFT","Contact":{"Name":"ABC Co","AccountNumber":"","EmailAddress":"","Addresses":{"AddressType":"POBOX","City":"City here","Region":"Region here","PostalCode":"AB1 2CD"}},"LineItems":[{"Description":"test","Quantity":"1","UnitAmount":"1","AccountCode":"200"},{"Description":"test","Quantity":"1","UnitAmount":"1","AccountCode":"200"}]}" array(3) { ["data"]=> array(3) { ["ErrorNumber"]=> int(14) ["Type"]=> string(24) "PostDataInvalidException" ["Message"]=> string(660) "JSON for post data was invalid,Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'Xero.API.Library.DataContracts.Addresses' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'Contact.Addresses.AddressType', line 1, position 132." } ["http_code"]=> int(400) ["error_msg"]=> string(30) "Error : Failed to post invoice" }

If I remove the 'Addresses', it works fine. I have added json_encode to the Addresses values, but no luck with that either.

Anyone have any ideas what I can do to fix this?

CodePudding user response:

According to the docs, Addresses needs to be an array of objects, you provided an object only. (You provided an associative array, to be precise, but encoding as JSON will turn that into an object.)

'Addresses' => (array( - just wrapping stuff into an additional set of braces does not create an additional array dimension, this needs to be 'Addresses' => array(array(

  •  Tags:  
  • php
  • Related