title: What is the better method to convert JSON to GeoJSON for Leaflet map
body: <p>I wanted to convert JSON to GeoJSON to plot divIcons on a Leaflet map from a JSON file that I get from an API. Being new to javascript I am having a really difficult time finding a working solution. I thought I had my issue resolved in my other post <a href="https://stackoverflow.com/questions/55887875/how-to-convert-json-to-geojson">How to convert JSON to GeoJSON
  </a> but that only solved one piece of the puzzle I was not aware of with getting the data loaded into the code that I thought was my issue. I am still greatly struggling with trying to get a working solution to convert JSON to GeoJSON so I am able to work with it from there to plot the divIcons on the map. </p>
  
  <p>I have done a lot of research both on here, YouTube and several other websites which most are outdated answers which don't seem to work or they do not apply to my situation. One of the methods I have tried for converting the JSON to GeoJSON is this. </p>
  
  <pre><code>$.getJSON("./data/tracking.json", function(jsonData) {
    var outGeoJson = {}
    outGeoJson['properties'] = jsonData
    outGeoJson['type']= "Feature"
    outGeoJson['geometry']= {"type": "Point", "coordinates":
        [jsonData['lat'], jsonData['lon']]}
  
    console.log(outGeoJson)
  }); 
  </code></pre>
  
  <p>While I thought it was working, after examining the console closer after getting some sleep I noticed that the coordinates are still coming up <code>Undefined</code> so it is not converting it to GeoJSON if though it is showing the data at the bottom, I still do not think it is loading it correctly. </p>
  
  <p><a href="https://i.stack.imgur.com/aHcdI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aHcdI.png" alt="enter image description here"></a></p>
  
  <p>The second method I tried, which I like, since I am able to pull in only the keys I need from the JSON file so I can use it in a popup is having the same results as the one above. </p>
  
  <pre><code>$.getJSON("./data/tracking.json", function(jsonData) {
  var geojson = {
    type: "FeatureCollection",
    features: [],
  };
  
  for (i = 0; i &lt; jsonData.positions.length; i++)
  
  
  
      geojson.features.push({
          "type": "Feature",
          "geometry": {
            "type": "Point",
            "coordinates": [jsonData.positions[i].longitude, jsonData.positions[i].latitude]
          },
          "properties": {
            "report_at": jsonData.positions[i].report_at,
            "lat": jsonData.positions[i].lat,
            "lon": jsonData.positions[i].lon,
            "dir": jsonData.positions[i].dir,
            "first": jsonData.positions[i].first,
            "last": jsonData.positions[i].last
          }
        });
  
      console.log(geojson)
      });
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/5zjJ4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5zjJ4.png" alt="enter image description here"></a></p>
  
  <p>That is what I have tried so far along with countless other methods that would make this post too long to list. Is there a better and newer way to accomplish this than what I am trying that is easier or is there something that I am just overlooking in the above code is new to this language? I really prefer that second method unless anyone has a much better suggestion for converting JSON to GeoJSON so I am able to parse that in my GeoJSON layer.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Here is a sample of the original JSON response from the API that I am trying to convert to GeoJSON</p>
  
  <pre><code>{
    "positions": [
      {
        "report_at": "2015-01-21 21:00:08",
        "lat": "38.9080658",
        "lon": "-77.0030365",
        "elev": "0",
        "dir": "0",
        "gps": "0",
        "callsign": "WX2DX",
        "email": "",
        "phone": "",
        "ham": "WX2DX",
        "ham_show": "0",
        "freq": "",
        "note": "",
        "im": "",
        "twitter": null,
        "web": "",
        "unix": "1421874008",
        "first": "William",
        "last": "Smith",
        "marker": "36181"
      },
      {
        "report_at": "2015-01-21 11:45:12",
        "lat": "39.2716141",
        "lon": "-76.6192093",
        "elev": "0",
        "dir": "0",
        "gps": "0",
        "callsign": "rldovejr",
        "email": null,
        "phone": null,
        "ham": null,
        "ham_show": null,
        "freq": null,
        "note": null,
        "im": null,
        "twitter": null,
        "web": null,
        "unix": "1421840712",
        "first": "Ron",
        "last": "Dove",
        "marker": "13573"
      }
  }
  </code></pre>