Read header file of .json and determine to which format it belongs to

Dani_S 5,261 Reputation points
2025-11-18T17:41:42.3866667+00:00

Hi,

Extension .json  format suits  to four formats: GeoJson, EsriJson, GeoJsonSeq, TopoJson.

How check the headers of these file and to determine to which format they  belong to ?

{ “.json”, “GeoJson” }, { “.json”, “EsriJson” }, { “.json”, “GeoJsonSeq” }, { “.json”, “TopoJson” },

 Please provide a ffull sample code for this operation in .NET 9 - C#.

I attached the sample files.

 

Thanks,

Dani

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

2 answers

Sort by: Most helpful
  1. Q&A Assist
    2025-11-18T17:42:27.62+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    To determine the specific format of a .json file (such as GeoJson, EsriJson, GeoJsonSeq, or TopoJson) based on its header, you can read the file's content and check for specific characteristics or keys that are unique to each format. Below is a sample code in C# that demonstrates how to perform this operation:

    using System;
    using System.IO;
    using Newtonsoft.Json.Linq;
    
    class Program
    {
        static void Main(string[] args)
        {
            string filePath = "path_to_your_file.json"; // Replace with your file path
            string format = DetermineJsonFormat(filePath);
            Console.WriteLine($"The format of the file is: {format}");
        }
    
        static string DetermineJsonFormat(string filePath)
        {
            if (!File.Exists(filePath))
            {
                throw new FileNotFoundException("File not found.", filePath);
            }
    
            string jsonContent = File.ReadAllText(filePath);
            JObject jsonObject = JObject.Parse(jsonContent);
    
            // Check for GeoJson
            if (jsonObject.ContainsKey("type") && jsonObject["type"]?.ToString() == "FeatureCollection")
            {
                return "GeoJson";
            }
    
            // Check for EsriJson
            if (jsonObject.ContainsKey("spatialReference") && jsonObject.ContainsKey("features"))
            {
                return "EsriJson";
            }
    
            // Check for GeoJsonSeq
            if (jsonObject.Type == JTokenType.Array)
            {
                return "GeoJsonSeq";
            }
    
            // Check for TopoJson
            if (jsonObject.ContainsKey("type") && jsonObject["type"]?.ToString() == "Topology")
            {
                return "TopoJson";
            }
    
            return "Unknown format";
        }
    }
    

    Explanation:

    1. File Reading: The code reads the content of the specified JSON file.
    2. JSON Parsing: It uses Newtonsoft.Json to parse the JSON content into a JObject.
    3. Format Checking: The code checks for specific keys and values that are characteristic of each format:
      • GeoJson: Looks for a type key with the value FeatureCollection.
      • EsriJson: Checks for spatialReference and features keys.
      • GeoJsonSeq: Determines if the JSON is an array.
      • TopoJson: Looks for a type key with the value Topology.
    4. Output: It prints the determined format.

    Make sure to install the Newtonsoft.Json package via NuGet to use this code.


    References:

    0 comments No comments

  2. Dani_S 5,261 Reputation points
    2025-11-19T06:28:59.3266667+00:00

    I'm adding the json files, I added .txt to extensions, since there is a limitation to load .json files here.


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.