0

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?

V4n1ll4
  • 5,973
  • 14
  • 53
  • 92
  • 1
    Is there any reason that you are using JSON as String inside LineItems ? – MMRahman Dec 15 '21 at 19:34
  • I think the double quotes are being added automatically by Zapier when trying to send the data. Not sure how to stop this happening. – V4n1ll4 Dec 15 '21 at 19:35
  • 1
    JSON and javascript objects are not the same thing. Right now you have a mixture of both, which isn't going to work. Zapier is sending JSON (assuming it's what's generating the contents of `LineItems`); you'll need to `JSON.parse` that before inserting it into the rest of your object. – Daniel Beck Dec 15 '21 at 19:37
  • If you are using JS Try to use RegExp to clean LineItems, Something Like this string.replace(/[\n"\&\r\t\b\f]/g, '\\$&'); – MMRahman Dec 15 '21 at 19:39
  • There is no reason to resort to regex here, and you'd likely break things in the process. `JSON.parse()` was designed for this purpose. – Daniel Beck Dec 15 '21 at 19:43
  • @DanielBeck agree with you, I was thinking he is already using JSON.parse(). However sometimes JSON.parse() fail to do so based on incoming data – MMRahman Dec 15 '21 at 19:46
  • When I use JSON.parse() Zapier throws an internal error "Error parsing response" so the JSON doesn't even get sent to Xero. – V4n1ll4 Dec 15 '21 at 19:57
  • @MMRahman "However sometimes JSON.parse() fail to do so based on incoming data" Only if the data is invalid JSON. In which case regex is going to cause even more problems. – Daniel Beck Dec 15 '21 at 21:50

2 Answers2

0

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"
    }
  ]
}
Serge
  • 40,935
  • 4
  • 18
  • 45
-1

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"
    }]
}
rustyskates
  • 856
  • 4
  • 10