2

Is it normal practice to change the type of a property from an array to a single item when there is only one occurrence?

[
  {
    "sport": [
      "Football",
      "Hockey"
    ]
  },
  {
    "sport": "Football"
  }
]

And how would you convert this data into programming objects when you don't know if it's an array or an object?

Relates to: How to handle both a single item and an array for the same property using JSON.net

Jogge
  • 1,654
  • 12
  • 36
  • I would just keep it an array. What are your considerations for making it a string with only a single item? I can only think of downsides. As the related question shows you need a custom JSON converter. So if you can choose how to generate the JSON Object I would keep it an array. – Merijndk Nov 05 '19 at 13:05
  • 1
    That makes sense. It's not normal practice as far as I know. I've never seen that practice as far as my memory serves me. I can't think of a reason to do this since the only benefit I can think of is a small code reduction because you don't have to write a loop. But the JSON converter code completly removes this benefit. – Merijndk Nov 05 '19 at 13:16
  • I do not want to do this. I'm in a place where i have to handle this situation, so i want to know if this is normal practice. – Jogge Nov 05 '19 at 13:16

0 Answers0