0

I am working on a WEB API and need to return json result in this format:

{
  "responseID": "b806d829-1d9c-4c69-8d7a-576ffc4de555",
  "orders": [
    {
      "id": order1,
      "status": "Issued",
      "statusDate": "2017-04-05T10:00:56.1549453+00:00",
      "amount": 100000,
      "quantity": 10,
      "fee": 99,
      "comments": "some comments"
    },
    {
      "id": order2,
      "status": "Declined",
      "statusDate": "2017-04-05T10:00:56.1549453+00:00",
    }
  ]
}

Can we prepare a json object like this where the objects within array has different attributes like in the above example order1 has 7 attributes but order2 has only 3.

My understanding is that the only way to achieve the desired results is to change the format of json object as below:

{
  "responseID": "b806d829-1d9c-4c69-8d7a-576ffc4de555",
  "issuesOrders": [
    {
      "id": order1,
      "status": "Issued",
      "statusDate": "2017-04-05T10:00:56.1549453+00:00",
      "amount": 100000,
      "quantity": 10,
      "fee": 99,
      "comments": "some comments"
    } ],
"declinedOrders": [
    {
      "id": order2,
      "status": "Declined",
      "statusDate": "2017-04-05T10:00:56.1549453+00:00",
    }
  ]
}

Is my understanding is correct?

Yaqub Ahmad
  • 27,569
  • 23
  • 102
  • 149
  • 2
    Once your `orders` property will be defined as `IEnumerable>` you'll get that results. Alternatively, if you're using Json.Net you can instruct the serializer to ignore properties that their values are `default(property_type)` – haim770 Apr 05 '17 at 11:44
  • 1
    I think the distinction made with status field is enough – levent Apr 05 '17 at 11:48
  • 1
    You can serialize whatever objects are in array, they might have completely different attributes. You can serializer array of `object`s with completely different objects. – Evk Apr 05 '17 at 12:01
  • This looks a bit like a duplicate of http://stackoverflow.com/questions/13588022/exclude-property-from-serialization-via-custom-attribute-json-net - maybe look at [JsonIgnore] attributes for the simplest answer. – Ian Robertson Apr 05 '17 at 12:04

0 Answers0