skip to Main Content

I have a json object as such:

{
 "data_type" : "trainings"
 "description" : "Training in Narewa"
 "district" : "Rewa"
 "gps_lat" : "-18.1295977111"
 "gps_lon" : "178.556147326"
 "id" : 24
 "location" : "Tavuya"
 "program" : "Training in Nadi"
 "project" : "Entrepreneurship"
 "province" : "Rewa"
}

how can I turn this into a geojson object?
Please note this is example within an array 12 objects with these key value pairs so most likely I’m going to loop the solution through 12 times.
I’ll need to use frontend javascript to achieve this

2

Answers


  1. You will most likely have to create a parser yourself. I think the best way to do this in the geojson format would be creating a feature list. I.E.

    {
        "type": "FeatureCollection",
        "features": [
          {
                "type": "Feature",
                "geometry": {
                    "type": "Point",
                    "coordinates": [
                        -18.1295977111,
                        178.556147326
                    ]
                },
                "properties": {
                    "data_type" : "trainings",
                    "description" : "Training in Narewa",
                    "district" : "Rewa",
                    "id" : 24
                    "location" : "Tavuya",
                    "program" : "Training in Nadi",
                    "project" : "Entrepreneurship",
                    "province" : "Rewa"
                }
          }, (add more for more entrys)
    
        ]
    }
    

    where properties acts as a way to store any custom data. So you could create a new json var doing

    var newDat = {};
    

    and then use .push to append new items that are in the geojson format above. As far as my knowledge geojson acts just as normal json files it just has a specific structure. I hope this helps :).

    Login or Signup to reply.
  2. var geojson = {
        "type": "FeatureCollection",
        "features": []
    };
    json.forEach(element =>{
        feature = {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    element.gps_lat,
                    element.gps_lon
                ]
            },
            "properties": element
            }
        geojson.features.push(feature);
    })
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search