codeforIATI / iati-datastore

An open-source datastore for IATI data with RESTful web API providing XML, JSON, CSV plus ETL tools
https://datastore.codeforiati.org
Other
1 stars 1 forks source link

Consistently output elements as arrays in JSON #381

Open markbrough opened 2 years ago

markbrough commented 2 years ago

Is your feature request related to a problem? Please describe.

As discussed on IATI Connect, there are advantages to outputting JSON data in a consistent format. However, at the moment, by default, Datastore Classic outputs a single element as an object rather than an array. For example, one sector:

<sector code="11220" />

Would be output as an object:

"sector": { 
  "code": "11220" 
}

Whereas two sectors:

<sector code="11220" percentage="50.0" />
<sector code="12220" percentage="50.0" />

Would be output as an array:

"sector": [
  { 
    "code": "11220",
    "percentage": "50.0"
  },
  { 
    "code": "12220" ,
    "percentage": "50.0"
  }
]

Describe the solution you’d like

It would be preferable to output elements in a consistent way. However: perhaps this should be done only for elements that can occur more than once. For example, <activity-status> can only occur once, so it should probably be output as an object.

In the xmltodict library, you can use the force_list parameter to force elements to be output as arrays.

If we were to implement this, it would probably need to be on a new v2 endpoint as it represents a significant change to the API. Alternatively, the output could be requested via an optional parameter, similar to the unwrap parameter that can be requested on the xml formatted endpoints.

Describe alternatives you’ve considered

Checking whether the values in an element are an array or an object.

Additional context

See e.g. sample JSON: https://datastore.codeforiati.org/api/1/access/activity.json?reporting-org=CA-3&limit=1&ref=qb&locale=en