[GIS] Seeking tool for converting JSON to GeoJSON or CSV

convertcsvgeojsonjson

How do you convert a JSON file to a GeoJSON or a CSV file? I have a JSON file with lat/lon information, but how can I can convert this file to GeoJSON or CSV. I have found some online tools to convert it to CSV, but I want to use it in a batchfile, so I need a tool.

The JSON file looks like this:

[
{"date":"2014-09-25","time":"20:49:09","lat":"53.269","lon":"6.935","depth":"3.0","place":"Meedhuizen","mag":"1.5","type":"GH","last":"True"},
{"date":"2014-09-24","time":"23:49:36","lat":"53.351","lon":"6.693","depth":"3.0","place":"Huizinge","mag":"1.0","type":"GH","last":"False"},
{"date":"2014-09-23","time":"17:34:48","lat":"53.242","lon":"6.728","depth":"3.0","place":"Lageland","mag":"1.0","type":"GH","last":"False"}
]

Best Answer

Look at the samples on the specification site. You'll need to write a script in the language of your choice that will get you from

[
{"date":"2014-09-25","time":"20:49:09","lat":"53.269","lon":"6.935","depth":"3.0","place":"Meedhuizen","mag":"1.5","type":"GH","last":"True"},
{"date":"2014-09-24","time":"23:49:36","lat":"53.351","lon":"6.693","depth":"3.0","place":"Huizinge","mag":"1.0","type":"GH","last":"False"},
{"date":"2014-09-23","time":"17:34:48","lat":"53.242","lon":"6.728","depth":"3.0","place":"Lageland","mag":"1.0","type":"GH","last":"False"}
]

to

{
  "type": "FeatureCollection",
  "features": [
    {
      "properties": {
        "last": "True",
        "place": "Meedhuizen"
        "time": "20:49:09",
        "depth": "3.0",
        "mag": "1.5",
        "date": "2014-09-25",
        "type": "GH"
      },
      "geometry": {
        "coordinates": [
          "6.935",
          "53.269"
        ],
        "type": "Point"
      },
      "type": "Feature"
    },
    {
      "properties": {
        "last": "False",
        "place": "Huizinge",
        "time": "23:49:36",
        "depth": "3.0",
        "mag": "1.0",
        "date": "2014-09-24",
        "type": "GH"
      },
      "geometry": {
        "coordinates": [
          "6.693",
          "53.351"
        ],
        "type": "Point"
      },
      "type": "Feature"
    },
    {
      "properties": {
        "last": "False",
        "place": "Lageland",
        "time": "17:34:48",
        "depth": "3.0",
        "mag": "1.0",
        "date": "2014-09-23",
        "type": "GH"
      },
      "geometry": {
        "coordinates": [
          "6.728",
          "53.242"
        ],
        "type": "Point"
      },
      "type": "Feature"
    }
  ]
}

in Python, for instance, I did this:

def convert_json(items):
    import json
    return json.dumps({ "type": "FeatureCollection",
                        "features": [ 
                                        {"type": "Feature",
                                         "geometry": { "type": "Point",
                                                       "coordinates": [ feature['lon'],
                                                                        feature['lat']]},
                                         "properties": { key: value 
                                                         for key, value in feature.items()
                                                         if key not in ('lat', 'lon') }
                                         } 
                                     for feature in json.loads(items)
                                    ]
                       })
Related Question