VOOZH about

URL: https://www.geeksforgeeks.org/python/convert-json-to-geojson-python/

⇱ Convert JSON to GeoJSON Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Convert JSON to GeoJSON Python

Last Updated : 23 Jul, 2025

GeoJSON has become a widely used format for representing geographic data in a JSON-like structure. If you have data in a standard JSON format and need to convert it into GeoJSON for mapping or analysis, Python provides several methods to make this conversion seamless. In this article, we will explore some commonly used methods to convert JSON to GeoJSON using practical code examples.

What is GeoJSON?

, short for Geographic JavaScript Object Notation, is an open standard format designed for representing simple geographical features along with their non-spatial attributes. It is based on JSON (JavaScript Object Notation) and is commonly used for encoding geographic data structures. GeoJSON is widely supported and can be easily parsed by various programming languages.

Here's an example of a simple GeoJSON Point feature, In this example:

  • The "type" property indicates that it's a GeoJSON Feature.
  • The "geometry" property specifies that the feature is a Point, and its coordinates are given in the "coordinates" property.
  • The "properties" property contains non-spatial attributes like the name and population of the location.
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [ -73.9857, 40.7484 ]
},
"properties": {
"name": "New York City",
"population": 8175133
}
}

Convert JSON to GeoJSON Python

Below, are the methods of Converting JSON to GeoJSON Python.

  • Using JSON & GeoJSON libraries
  • Using LineString GeoJSON
  • Using Polygon GeoJSON

Convert JSON to GeoJSON Using JSON and GeoJSON libraries

In this example, below Python script defines a function convert_json_to_geojson that takes JSON data as input, parses it, extracts the "geometry" and "properties" fields, and converts it into a GeoJSON Feature with a Point geometry. The script then creates a GeoJSON FeatureCollection containing the converted Feature and prints the resulting GeoJSON with proper indentation.

Output

{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
0,
0
]
},
"properties": {
"name": "Example Feature"
}
}
]
}

Convert JSON to GeoJSON Using LineString GeoJSON

In this example ,below Python script uses the geojson library to create a GeoJSON LineString from a set of coordinates [(0, 0), (1, 1), (2, 2)]. It then constructs a GeoJSON Feature using the LineString and creates a FeatureCollection containing that Feature. Finally, the script prints the resulting GeoJSON with proper indentation.

Output :

{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[0, 0],
[1, 1],
[2, 2]
]
},
"properties": null
}
]
}

Convert JSON to GeoJSON Using Polygon GeoJSON

In this example, below Python script uses the geojson library to create a GeoJSON Polygon from a set of coordinates representing a square [(0, 0), (0, 1), (1, 1), (1, 0), (0, 0)]. It then prints the resulting GeoJSON Polygon with proper indentation.

Output

{
"type": "Polygon",
"coordinates": [
[
[0, 0],
[0, 1],
[1, 1],
[1, 0],
[0, 0]
]
]
}

Conclusion

In conclusion, the provided Python scripts demonstrate the process of converting JSON data into GeoJSON format using the geojson library. The scripts showcase the creation of GeoJSON objects, such as Point, LineString, and Polygon, from corresponding JSON structures. These examples illustrate how to extract relevant geometry and properties information, construct GeoJSON features, and organize them into FeatureCollections for spatial representation.

Comment