I really don't understand my problem :
I have geojson files, I need to get the content and return a valid variable.
My test :
//this works
var obj_valid = {"type": "FeatureCollection","crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },"features": [{ "type": "Feature", "properties": { "id": "14001", "nom": "val1" }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 0.301, 49.371 ], xxx , [ 0.301, 49.371 ] ] ] ] } }]};
//doesn't work
var obj_fail = $.getJSON("geojson/com/14001.json");
I would like to return the same content that what is in my first var "obj_valid". I tried $.getJSON, $.ajax, $.getScript but without success, the output is different :
What is my mistake ??
Thanks in advance for your help,
$.getJSON is an ajax request and is asynchrounous. It doesn't return data , it returns a promise.
You need to consume the response data in a success or done callback
$.getJSON("geojson/com/14001.json", function(responseData){
// do something with responseData
});
// OR
$.getJSON("geojson/com/14001.json").done(function(responseData){
// do something with responseData
});
See $.getJSON docs
Shouldn't it be like this
var obj_fail = {};
$.getJSON("geojson/com/14001.json")
.done(function(data){
obj_fail = data;
});
Hope this helps!
Related
I'm having a bit of a problem here, I'm creating a Leaflet project and need to access to data outside of a variable, in my case a polygon. Until now I have the data inside the polygon with
data:[28.63,34.29,12.23,9.33,6.97,3.93,4.63]
but I'd prefer the data to be outside of this variable with
var koeln = [28.63,34.29,12.23,9.33,6.97,3.93,4.63];
I'm now looking for a result how to access to the var koeln inside of the data, I've tried to put it in brakes or other things, but nothing seems to work..
edit:
var wahl2017 = { "type": "FeatureCollection", "features": [ { "type": "Feature", "properties":
{"name":"Wahlbezirk 13","partei":"cdu","wahlbeteiligung":69.42, "drittpa1":"gruen","drittpa2":"fdp", data:[28.63,34.29,12.23,9.33,6.97,3.93,4.63],title:"Köln I"}, "geometry": { "type": "Polygon", "coordinates": [ [ [6.976650953292847, 50.84119137806263 ],
(and so on)
Refering to your comment, you have this data structure:
var wahl2017 = {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": {
"name": "Wahlbezirk 13",
"partei": "cdu",
"wahlbeteiligung": 69.42,
"drittpa1": "gruen",
"drittpa2": "fdp",
data: [28.63, 34.29, 12.23, 9.33, 6.97, 3.93, 4.63],
title: "Köln I"
}
}]
};
You can access the data like this:
let koeln = wahl2017.features[0].properties.data
You can access the data variables with var koeln = wahl2017.features[0].properties.data
When you have more cities you can call .forEach()
var cities = [];
wahl2017.features.forEach(function(city){
cities.push({name: city.properties.name, data: city.properties.data});
})
console.log(cities);
console.log(cities[0].data);
I have below code which is going to invoke REST endpoint and return response back. I just tried to print the response.body in the console and It works perfectly fine.
var express = require('express');
var app = express();
var PORT = 8001;
var request = require('request');
var HashMap = require('hashmap');
var endPoint="http://sandbox.dev.amazon.com/idsearch/environment/amazon/";
app.get('/productId/:proId',async (req,res) => {
try
{
var headers ={
"accept":"application/json"
}
var options = {
url:endPoint.concat(req.params.proId),
headers:headers
}
request(options, (error,response,body)=> {
console.log(response.body) // It returned response as below output JSON file
res.send("STATUS CODE : 200");
});
}
catch(error)
{
throw error;
}
});
Output:
{
"<Some dynamic Content>": {
"type": "PROD-ID",
"environment": "amazon",
"tags": [
{
"name": "EC-6S0005704A8324S98020",
"source": "amazonstage2ma_paymentapiplatserv#TOKEN",
"flags": [
"FLAG_DYNAMIC_VALUE",
"FLAG_ID_LOOKUP_SUPPORTED"
]
}
],
"callSummary": [
{
"pool": "slingshotrouter",
"machine": "stage21007",
"apiName": "GET",
"status": "0",
"duration": 13400.0,
"link": "https://www.amazon.qa.pilot.com/Tid-942342192424j2j234"
},
{
"pool": "slingshot",
"machine": "stage21029",
"apiName": "GET",
"status": "1",
"duration": 13368.0,
"link": "https://www.amazon.qa.pilot.com/Tid-12342342i842424j2j234"
},
{
"pool": "devstage_userbridgedomainserv",
"machine": "amazon1int-g_userbridgedomainserv_22",
"apiName": "POST",
"status": "1",
"duration": 15.0,
"link": "https://www.amazon.qa.pilot.com/Tid-02341723424i842424j2j290"
}
],
"partial": false
}
}
The above output contains all the responses with respective Endpoint URL which is expected. But I just want to fetch only the object contains "Status: 1". I'm just wondering that How can I manipulate the response.body object to get the below JSON as output.
Expected Output:
{
"callSummary":[
{
"pool": "slingshot",
"machine": "stage21029",
"apiName": "GET",
"status": "1",
"duration": 13368.0,
"link": "https://www.amazon.qa.pilot.com/Tid-12342342i842424j2j234"
},
{
"pool": "devstage_userbridgedomainserv",
"machine": "amazon1int-g_userbridgedomainserv_22",
"apiName": "POST",
"status": "1",
"duration": 15.0,
"link": "https://www.amazon.qa.pilot.com/Tid-02341723424i842424j2j290"
}
]
}
I just want to iterate the response.body obj and check the status as 1 if it's then I need to fetch all the details and form it as above payload. This is dynamic content but the template format is static.
I tried the below code to iterate the response.body but no luck.
var string = JSON.stringify(response.body);
var objectValue = JSON.parse(string);
var obj = objectValue.callSummary;
console.log(obj.length); // It returned undefined.
Please lead me to achieve this.
To return that json, just
res.json(response.body['<Some dynamic Content>'].callSummary);
Adding some validation and error handling would be a good idea before sending the response.
In case your key is just an example and you never know your first key value, and you always want the values of the first key
res.json(Object.values(response.body)[0].callSummary);
Object.values returns an array, so you can iterate the values if you want manage more than the first one
I have this js variable which is parsing a json content inline:
var places = {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": {
"icon": "theatre"
},
"geometry": {
"type": "Point",
"coordinates": [-77.038659, 38.931567]
}
}]
};
I need this content to be inside an external json file since it is a long list to load dynamically.
Can you tell me how to call the json file (the code must be as it is and not parsed)? I tried dozens of solution here but without results :(
Thanks in advance for your help
I am wondering if I have a GeoJSON object such as
var geoJSONLoc= {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"timeReported": "2013-01-22 08:42:26+01"
},
"geometry": {
"type": "Point",
"coordinates": [
7.582512743,
51.933292258,
1
]
}
},
{
"type": "Feature",
"properties": {
"timeReported": "2013-01-22 10:00:26+01"
},
"geometry": {
"type": "Point",
"coordinates": [
7.602516645,
51.94962073,
1
]
}
}]}
How can I save this into a .json file for future use?
I am trying to use
localStorage.setItem('geoJSONLoc.json', geoJSONLoc);
but it doesnt seem to be the right answer.
I've done a little work in NodeJS, so this maybe way off base.
But couldn't you use the fs module for this?
something like
var fs = require('fs')
fs.writeFile('geoJSONLoc.json', geoJSONLoc, function(err){
if(err){
console.log(err);
}
});
I'm working with the form builder Alpaca, and I would like to interpret a javascript function within a json datasource file, to select a certain file :
/data/options.json :
"nature":{
"type": "select",
"dataSource": "function(e) {...}"
},
This file is loaded here :
/test.html :
$("#div").alpaca({
"optionsSource": "/data/options.json",
etc...
Is this possible ?
Thanks.
You can use eval as said :
object = {"nature":{
"type": "select",
"dataSource": "function(e) {alert('ok')}"
}
}
var x = eval("("+object.nature.dataSource+")");
x()
Demo
You could make the function call first, then add the result to you JSON with data.nature.push();
See this answer : Add data to JSON in JS
script type="text/javascript">
var JSON = {"nature":{
"type": "select"
}};
JSON.dataSource.push(function());
$("#form1").alpaca(JSON);
</script>
As the Alpace datasources doc, http://www.alpacajs.org/docs/api/datasources.html you can use custom function in the datasource parameter :
$("#field5").alpaca({
"schema": {
"type": "string",
"title": "Pick an Action Hero"
},
"options": {
"type": "select",
"dataSource": function(callback) {
callback([{
"value": "rambo",
"text": "John Rambo"
}, {
"value": "norris",
"text": "Chuck Norris"
}, {
"value": "arnold",
"text": "Arnold Schwarzenegger"
}]);
}
}
});