9

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"}
]

3 Answers 3

13

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)
                                    ]
                       })
2

You may try ogr2ogr(from GDAL) and jq, which are quite ubiquitous, at least on Linux derivate.

1/ convert from json to csv using jq:

cat input.json | jq -r '(map(keys) | add | unique) as $cols | map(. as $row | $cols | map($row[.])) as $rows | $cols, $rows[] | @csv' > input.csv

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

2/ Create a Virtual file (VRT) for you CSV

<OGRVRTDataSource>
  <OGRVRTLayer name="input">
    <SrcDataSource>input.csv</SrcDataSource>
    <GeometryType>wkbPoint</GeometryType>
    <LayerSRS>WGS84</LayerSRS>
    <GeometryField encoding="PointFromColumns" x="lon" y="lat"/>
  </OGRVRTLayer>
</OGRVRTDataSource>

3/ Convert your VRT to GeoJSON

ogr2ogr -f GeoJSON output.geojson input.vrt input

Starting with gdal 2.1, you may convert directly from json to geojson (not tested)

1

This can be done on the command line without needing any code using two Node libraries.

Install them with:

npm install -g json2csv csv2geojson

Then simply:

cat file.json | json2csv | csv2geojson > file.geojson

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.