I have a .json file that I'm trying to import as a text string.
This is the exact file contents:
{ "type": "Polygon", "coordinates": [ [ [ 1.5887868, 50.8315241 ], [ 1.5830702, 50.8466931 ]....... ] ] ] }
I'm trying to import it using this:
jsonString = jQuery.get('./polygons/1.json', function(data) {
console.log(data);
});
However, console.log(data) is coming across as an array, and I don't want it to - I just want the literal text string, just as it is in the file.

How do I accomplish this?
Update
So my entire code is:
jsonString = jQuery.get('./polygons/1.json', function(data) {
return JSON.stringify(data);
});

But now I get this screenshot below:
I guess it's the responseText that I'm trying to get out. Why is jQuery.get adding all this additional info into my file? What am I doing wrong?
Second Update
I always feel bad when someone takes their time to explain something to me, and I don't understand it. So, this whole thing exists so that I can pull dynamic data into jsonString, depending on what the user wants.
Right now though, I'm having to accomplish this feat by making individual files with the geodata coordinates, then utilizing it using map.data.loadGeoJson('./maps_polygons/' + territory + '.json');
function mapClick(data) {
territory = data; //the section of the map that the user clicks on, e.g. `california` or `texas`
var mapOpt = {
center:new google.maps.LatLng(50.477971, 0.482999),
zoom:8,
};
map = new google.maps.Map(document.getElementById("googleMap"),mapOpt);
map.data.loadGeoJson('./maps_polygons/' + territory + '.json');
google.maps.event.addDomListener(window, 'load', initialize);
}
Now, this WORKS, but I felt that it must be incredibly inefficient, so I wanted to hold all of the coordinates in a single file, and bring them in dynamically as the main input from function mapClick(data) changed. This has GOT to be possible, and from what you've all shown me, I can write an ajax query, or use jQuery.get from within a function, and that will bring the json string in, but that does me no good, because it's stuck inside of that function and I can't get to it.
This is my frustration...
Final Update
Okay - shout-out to this page and this page, as well as @Lye-Fish for finally helping me understand how this all works.
In searching SO tonight, I found tons of posts about aspects of this topic, so I wanted to die it all in how I finally got these two things to work:
- The idea of getting something out of an ajax call (the original reason for my post)
- How to tie that back into the Google Maps API.
The HTML
<area shape="poly" coords="..." href="#" alt="california" onClick="mapClick(alt)"/>
The Javascript
function mapClick(data) {
territory = data;
function getPolygons(territory, passTodoWork) {
jQuery.get('./maps_polygons/'+ territory +'.json', function(data, status, jqXHR) {
var coordinates = JSON.parse(jqXHR.responseText);
passTodoWork(territory,coordinates);
});
}
function drawMap(firstItem, secondItem) {
var mapOpt = {
center:new google.maps.LatLng(50.367247, 2.745326),
zoom:8,
};
map = new google.maps.Map(document.getElementById("googleMap"),mapOpt);
map.data.addGeoJson(secondItem);
map.data.setStyle({
fillColor: '#2b86cc',
strokeColor: '#1e5c8c'
});
google.maps.event.addDomListener(window, 'load', initialize);
}
getPolygons(territory, drawMap);
}
note, I don't utilize territory in the final drawMap function - that's part of another piece of code that made this all too long to prove the point...
console.log(JSON.stringify(data))