Home > Software design >  JSON is valid but not working when sent to Xero
JSON is valid but not working when sent to Xero

Time:12-16

I have the below JSON which has been validated with https://jsonlint.com/. However, when I run this through Xero API, it throws an error, shown below.

{
  "Type": "ACCREC",
  "Status": "AUTHORISED",
  "DueDate": "2021-12-11T14:24:08Z",
  "InvoiceNumber": "PRO152125",
  "Contact": {
    "ContactID": "ddd-05f9-46dd-b4a6-2ccbb5deb330"
  },
  "LineItems": [
    "{\"Description\": \"test1\", \"Qty\": 0.30, \"UnitAmount\": 950.0, \"TaxType\": \"OUTPUT2\", \"AccountCode\": \"200\"},\n{\"Description\": \"test2\", \"Qty\": 0.30, \"UnitAmount\": 300.0, \"TaxType\": \"OUTPUT2\", \"AccountCode\": \"200\"}"
  ]
}

{
  "ErrorNumber": 14,
  "Type": "PostDataInvalidException",
  "Message": "JSON for post data was invalid,Error converting value \"{\"Description\": \"test1\", \"Qty\": 0.30, \"UnitAmount\": 950.0, \"TaxType\": \"OUTPUT2\", \"AccountCode\": \"200\"},\n{\"Description\": \"test2\", \"Qty\": 0.30, \"UnitAmount\": 300.0, \"TaxType\": \"OUTPUT2\", \"AccountCode\": \"200\"}\" to type 'Xero.API.Library.DataContracts.LineItem'. Path 'LineItems[0]', line 1, position 417."
}

Can anyone help with why this is happening?

CodePudding user response:

API are trying to deserialize your json to the class like

class MyClass 
{ 
    ..properties

  List<LineItem> LineItems...
}

class LineItem
{
  ... properties inside of the string
}

your json is valid but it is

class MyClass 
{ 
    ..properties

  List<string> LineItems...
}

Api serializer can not convert List< string > to List< LineItem >. This is what causes the error

you can fix json this way

  var jsonObject=GetFixedJsonObject(json);
  
  var fixedJson=jsonObject.ToString();


public JObject GetFixedJsonObject(string json)
{
    var jsonObject = JObject.Parse(json);
    
    var jsonLineItems = "["   (string)jsonObject["LineItems"][0]   "]";
    
    jsonObject["LineItems"] = JArray.Parse(jsonLineItems);
    
    return jsonObject;
}

fixed json

{
  "Type": "ACCREC",
  "Status": "AUTHORISED",
  "DueDate": "2021-12-11T14:24:08Z",
  "InvoiceNumber": "PRO152125",
  "Contact": {
    "ContactID": "ddd-05f9-46dd-b4a6-2ccbb5deb330"
  },
  "LineItems": [
    {
      "Description": "test1",
      "Qty": 0.3,
      "UnitAmount": 950.0,
      "TaxType": "OUTPUT2",
      "AccountCode": "200"
    },
    {
      "Description": "test2",
      "Qty": 0.3,
      "UnitAmount": 300.0,
      "TaxType": "OUTPUT2",
      "AccountCode": "200"
    }
  ]
}

CodePudding user response:

You've got quotes around your line item, meaning it's just being seen as one big string. Try this:

{
    "Type": "ACCREC",
    "Status": "AUTHORISED",
    "DueDate": "2021-12-11T14:24:08Z",
    "InvoiceNumber": "PRO152125",
    "Contact": {
        "ContactID": "ddd-05f9-46dd-b4a6-2ccbb5deb330"
    },
    "LineItems": [{

        "Description": "test1",
        "Qty": 0.30,
        "UnitAmount": 950.0,
        "TaxType": "OUTPUT2",
        "AccountCode": "200"
    }, {
        "Description": "test2",
        "Qty": 0.30,
        "UnitAmount": 300.0,
        "TaxType": "OUTPUT2",
        "AccountCode": "200"
    }]
}
  • Related