Leaflet,js - how do I create the JSON file?

I've used Leaflet.js before...at my last job. We had a JSON file that held the longitude and latitude. It was generated using C# code.

I'm following this example https://asmaloney.com/2014/01/code/creating-an-interactive-map-with-leaflet-and-openstreetmap/

In our database, we have a list of addresses. How can I generate a JSON file of those addresses? In the link above, it's in "The JSON Data" section of the site. How is that file generated?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
Not sure what server side language you're using, so I can't give you specifics but the JSON file is just an array of objects - each object having 4 properties - name, url, lat, lng. You would need to query the database and convert the results to JSON. In PHP, a simple example might look something like this::

$db = new PDO("mysql:host=localhost;dbname=yourDb", "username", "password");
$query = $db->query("SELECT name, url, lat, lng FROM yourTable");
$rows = $query->fetchAll(PDO::FETCH_OBJ);
echo json_encode($rows);

Open in new window

CamilliaAuthor Commented:
You would need to query the database and convert the results to JSON
This should get me going. Let me take a closer look.

I'm using C#
Chris StanyonWebDevCommented:
OK. The principle remains the same. You may want to install JSON.NET (available through nuget). Once you've queries you database and have an array of objects, then you'd serialize it:

var json = JsonConvert.SerializeObject(yourObjectArray);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CamilliaAuthor Commented:
Thanks, chris. I'll work on it tonight. Thanks for helping me out.
CamilliaAuthor Commented:
thanks :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.