1

I've got a 139,000 line JSON file, whose structure basically looks like this (it's an extract from OpenStreetMap):

{
  "type": "FeatureCollection",
  "generator": "overpass-ide",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "@id": "relation/7859",
        "TMC:cid_58:tabcd_1:Class": "Area",
        "TMC:cid_58:tabcd_1:LCLversion": "9.00",
        "TMC:cid_58:tabcd_1:LocationCode": "4934",
        "leisure": "park",
        "name": "Platnersberg",
        "type": "multipolygon",
        "@geometry": "center"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          11.128184,
          49.4706035
        ]
      },
      "id": "relation/7859"
    },
    {
      "type": "Feature",
      "properties": {
        "@id": "relation/62370",
        "TMC:cid_58:tabcd_1:Class": "Area",
        "TMC:cid_58:tabcd_1:LCLversion": "8.00",
        "TMC:cid_58:tabcd_1:LocationCode": "1157",
        "admin_level": "6",
        "boundary": "administrative",
        "de:place": "city",
        "name": "Eisenach",
        "type": "boundary",
        "@geometry": "center"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          10.2836229,
          50.9916015
        ]
      },
      "id": "relation/62370"
    }
  ]
}

Not I would like to get the name, the TMC location code, and the coordinates of every feature in this file, preferably as a CSV file:

location_code,name,latitude,longitude

I know I can make a regex, which kicks out all superfluous nodes, but it's gonna be a rather complicated one. I've also got the jq tool installed on an OpenSuSE Leap 15.1 machine here, but I'm a greenhorn when it comes to this tool.

Any ideas on how to to do this extraction job?

1 Answer 1

2

I'm a greenhorn myself, but I think something like

$ jq -r '.features[] | select(.type == "Feature") | [.properties."TMC:cid_58:tabcd_1:LocationCode",.properties.name,.geometry.coordinates[]] | @csv' file.json
"4934","Platnersberg",11.128184,49.4706035
"1157","Eisenach",10.2836229,50.9916015

should do it. The select(.type == "Feature") filter may not be necessary - I'm not sure if any other type is possible.

1
  • OK, thank you. Your solution is exactly what I've been searching for.
    – Neppomuk
    Commented Oct 13, 2020 at 20:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .