expressjs / expressjs/body-parser
support for ndjson
- Dominant language
- JavaScript
- Stars
- 5.5k
- Forks
- 767
- Avg merge
- 13h 53m
- Merged PRs (30d)
- 10
Description
It would be nice if the `json` middleware could support [ndjson](http://ndjson.org/) 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.
Contributor guide
Assessment
This issue has not been assessed yet.