Nick Kinlen
Nick Kinlen

Reputation: 1406

Convert JSON into GeoJSON compatible with Mapbox Studio

I'm attempting to use this JSON from a SpaceX API to display the locations of all the SpaceX launch sites on a Mapbox map using Mapbox-GL. When I attempt to load this into a dataset in Mapbox Studio I get an error that says: Input failed. "type" member required on line 1.

I assume this is due to the way that the JSON is structured i.e. it doesn't have GeoJSON properties.

How can I easily adapt this JSON and convert it into GeoJSON that works with Mapbox?

Upvotes: 1

Views: 2446

Answers (1)

Scarysize
Scarysize

Reputation: 4281

The JSON file you provided isn't a valid GeoJSON. You can read more about the specification of the format here: http://geojson.org/

You would want a small script to transform the SpaceX JSON file into valid GeoJSON. Currently a single record looks like this:

{
  "id": "ccafs_slc_40",
  "full_name": "Cape Canaveral Air Force Station Space Launch Complex 40",
  "status": "active",
  "location": {
    "name": "Cape Canaveral",
    "region": "Florida",
    "latitude": 28.5618571,
    "longitude": -80.577366
  },
  "vehicles_launched": [
    "Falcon 9"
  ],
  "details": "SpaceX primary Falcon 9 launch pad, where all east coast Falcon 9s launched prior to the AMOS-6 anomaly. Initially used to launch Titan rockets for Lockheed Martin. Back online since CRS-13 on 2017-12-15."
}

What you probably want is a Feature with a geometry type of Point like this:

{
  "type": "Feature",
  "geometry": {
    "type": "Point",
    "coordinates": [-80.577366, 28.5618571]
  },
  "properties": {
    "id": "ccafs_slc_40",
    "full_name": "Cape Canaveral Air Force Station Space Launch Complex 40",
    "status": "active",
    "location": {
      "name": "Cape Canaveral",
      "region": "Florida"
    },
    "vehicles_launched": ["Falcon 9"],
    "details":
      "SpaceX primary Falcon 9 launch pad, where all east coast Falcon 9s launched prior to the AMOS-6 anomaly. Initially used to launch Titan rockets for Lockheed Martin. Back online since CRS-13 on 2017-12-15."
  }
}

After you transformed each record of your original array, you need to wrap them in a FeatureCollection in order for mapbox-gl to render it:

{
  "type": "FeatureCollection",
  "features": [
    //...
  ]
}

Upvotes: 3

Related Questions