AFAIK, NDJSON has no official magic header or MIME signature, so detection would be heuristic. The structural rule is that the file consists of multiple valid JSON values separated by newline characters, usually objects. That means you should read line by line and attempt to parse each non-empty line as independent JSON.
One possible detection strategy is to require at least N valid JSON lines and allow a small number of non-JSON lines such as comments or whitespace. You should not rely on checking only the first character because valid JSON values may start with ", digits, t, f, or n. The safest approach would be probably actual JSON parsing per line.
Try this:
using System;
using System.IO;
using System.Text.Json;
public static class NdjsonDetector
{
public static bool LooksLikeNdjson(string content, int minJsonLines = 2, int maxNonJsonLines = 1)
{
if (string.IsNullOrWhiteSpace(content))
return false;
int jsonLines = 0;
int nonJsonLines = 0;
using var reader = new StringReader(content);
string line;
while ((line = reader.ReadLine()) != null)
{
var trimmed = line.Trim();
if (trimmed.Length == 0)
continue;
if (IsValidJson(trimmed))
{
jsonLines++;
if (jsonLines >= minJsonLines)
return true;
}
else
{
nonJsonLines++;
if (nonJsonLines > maxNonJsonLines)
return false;
}
}
return jsonLines >= minJsonLines;
}
private static bool IsValidJson(string text)
{
try
{
using var doc = JsonDocument.Parse(text);
return true;
}
catch
{
return false;
}
}
}
If you want stricter NDJSON detection you could add extra rules such as rejecting files whose first non-empty character is [ because that indicates a normal JSON array rather than newline-delimited records, and requiring every parsed line’s root element to be JsonValueKind.Object.
if (doc.RootElement.ValueKind != JsonValueKind.Object)
return false;
Btw. your sample is detected as NDJSON because it contains multiple newline-separated JSON objects rather than a single array structure, which satisfies the minimum count heuristic. The key principle is line-by-line JSON parsing, not string pattern checks.
If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.
hth
Marcin