body-parser icon indicating copy to clipboard operation
body-parser copied to clipboard

support for ndjson

Open pinkasey opened this issue 2 years ago • 0 comments

It would be nice if the json middleware could support ndjson as well. e.g, something like this as input:

{ "id": 1, "message": "hello" }
{ "id": 2, "message": "I'm a JSON inside a NDJSON" }

I've recently tried to consume a webhook of some big SaaS company, and they call the webhook with a POST and Content-Type: application/json (they should be passing Content-Type: application/x-ndjson).

From my perspective - I didn't even know that ndjson exist until yesterday, and it's so similar to json - I would've hoped anything that can consume json can also consume ndjson. It's very easy to make that work.

What I had in mind - add an option, something like allow_ndjson (default to false for backward compatibility), and if it's true - when parsing a JSON fails, try splitting the body by \n, and parsing each part as json. return an array of the resulting jsons. So the example above would result it:

[
  { "id": 1, "message": "hello" },
  { "id": 2, "message": "I'm a JSON inside a NDJSON" }
]

I'll be happy to contribute PR if there's a chance it would be approved.

pinkasey avatar Dec 18 '22 19:12 pinkasey