Share via

How to detect .ndJson

Y. Yakir 70 Reputation points
2026-02-05T20:52:09.8033333+00:00

Hi,

NDJSON (usually written as .ndjson) stands for Newline-Delimited JSON.

instead of one big JSON array, you have one JSON object per line.

How to detect .ndJson file ?

This my code.

Thanks in advance,

User's image

Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

Answer accepted by question author
  1. Marcin Policht 81,395 Reputation points MVP Volunteer Moderator
    2026-02-05T22:19:54.92+00:00

    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

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. AgaveJoe 31,176 Reputation points
    2026-02-06T22:20:44.1333333+00:00

    The IsValidJson() method is designed to validate a single JSON object. Because NDJSON contains multiple independent JSON objects separated by newlines, JsonDocument.Parse() will throw an exception the moment it sees data following the first closing brace.

    Here is a console demo to illustrate the difference. Notice how we must iterate through the lines to validate the file correctly:

    using System.Text.Json;
    
    string content = "{\"type\":\"Feature\",\"properties\":{}}\n{\"type\":\"Feature\",\"properties\":{}}\n";
    
    Console.WriteLine("--- Testing Individual Lines ---");
    foreach (var line in content.Split('\n', StringSplitOptions.RemoveEmptyEntries))
    {
        Console.WriteLine(IsValidJson(line) ? $"Valid: {line}" : $"Invalid: {line}");
    }
    
    Console.WriteLine("\n--- Testing Entire NDJSON String (Will Fail) ---");
    if (!IsValidJson(content))
    {
        Console.WriteLine("Validation failed as expected: NDJSON is not a single JSON object.");
    }
    
    static bool IsValidJson(string text)
    {
        try
        {
            using var doc = JsonDocument.Parse(text);
            return doc.RootElement.ValueKind == JsonValueKind.Object;
        }
        catch (JsonException)
        {
            // JsonDocument throws if it finds multiple roots or malformed data
            return false;
        }
    }
    
    

  2. Jack Dang (WICLOUD CORPORATION) 14,340 Reputation points Microsoft External Staff Moderator
    2026-02-06T08:57:48.2733333+00:00

    Hi @Y. Yakir ,

    Thanks for reaching out.

    1. Namespace for JsonDocument and JsonValueKind Since you’re on .NET Standard 2.1, you need:
    using System.Text.Json;
    

    Make sure your project references System.Text.Json (it’s included in .NET Standard 2.1). No extra NuGet package is needed unless you removed it.

    1. Where to add the root element check The line
    if (doc.RootElement.ValueKind != JsonValueKind.Object) 
        return false;
    

    goes inside the IsValidJson method, right after parsing the JSON, like this:

    private static bool IsValidJson(string text)
    {
        try
        {
            using var doc = JsonDocument.Parse(text);
            // Ensure the JSON root is an object, not an array or primitive
            if (doc.RootElement.ValueKind != JsonValueKind.Object)
                return false;
            return true;
        }
        catch
        {
            return false;
        }
    }
    

    Hope this helps! If my answer was helpful - kindly follow the instructions here so others with the same problem can benefit as well.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.