re: Nice post! import json from csv import DictWriter with open("data.json", "r") as f: users = json.load(f) cols = ["name", "creditcard"] with...

Having arrays on the top-level of JSON documents is indeed valid although it is definitely an anti-pattern. By doing so you block yourself from adding any meta information in the future.
If you build an API, you always want to wrap an array in an object. Then you can add additional fields like possible errors or pagination later on.

  "data": [],
  "status": "not ok",
  "error": { "code": 123, "message": "..." },
  "page": 42

Personally, I'd prefer the array in most cases. If I call an endpoint called customers, I would expect it to return an array of customers, not something that contains such an array, might or might not have an error and so on.
If I want to stream the response, I'd also be better off with an array, because whatever streaming library I use probably supports it.

Code of Conduct Report abuse