How to minify Overpass API output in JSON format? - json

I am interested to get all roads near a certain point, and I need just the geometry of the data because I need to visualize a simple map. This is the Overpass query I use the get the roads:
[out:json];
(way(around:1000.0,LAT,LONG)["highway"~"^(motorway|trunk|primary|secondary|tertiary|residential|unclassified|tertiary_link|secondary_link|primary_link|trunk_link|motorway_link|living_street|service|pedestrian|track|path)$"];);
out geom;
I'm trying to optimize the result, for example disable the json pretty print to reduce returned data in KB, or remove other things that I'm not interested with.

Related

Accessing nedb results

The question is: how to access multiple documents returned from a nedb 'find' command.
Understanding some insight I gained using nedb in nodejs.
Regarding docs returned via nedb find and accessing key value pair:
Thought this would work: docs.current.temp_f but it doesn't.
This works: docs[0].current.temp_f does work.
Background and detail:
I'm new to JavaScript, nodejs, nedb, and have never used mongodb, but understand nedb has a similar API.
The reason for my post is: I use this site a lot, but could not find an answer to my problem. I finally figured it out myself, and am posting it here so I can return a contribution to the community.
I'm accessing Wunderground using their API. Works great. I then pushed the results into nedb using the nedb API's 'insert' command. There are multiple examples of this available, here and elsewhere.
Using the nedb API's find command, I was able to retrieve my data. I could print it out using console.log(). Again, multiple examples here and elsewhere for this.
Wunderground returns a JSON object (if requested in this format), in the form {response:{...some data}, current_observation:{...some data...temp_f...some more data}}, which can be inserted into the nedb database.
Using the nedb API 'find' command (e.g.,
db.find({},function(err,docs) {console.log(docs);} I was able to print out my data. But my desire was to just obtain a few key:value pairs, not the whole structure. I thought that something like docs.current_observation.temp_f would do the trick, but this was always 'undefined' and caused an error.
What I didn't understand, because I'm so new in this area, is that nedb is returning a "set" of data; an array of JSON objects in this case, since I was inserting JSON objects. I never saw this mentioned anywhere on the npmjs.com site for nedb. So I wanted to alert others like myself that lacked this knowledge.
Here's what ultimately worked to obtain the key:value information:
console.log("temp_f: ", docs[0].current_observation.temp_f);
where I'm accessing the array element 0.
Of course, this would probably end up in something like a for/in loop.
I would be interested in seeing any additional ideas on retrieving key:values from the result set returned, if you know of any.
//Thought this would work, but it doesn't
docs.current_observation.temp_f
//but this does, where x is a value to record
docs[x].current_observation.temp_f
Simple answer is use mongo's query language with find method to return an array of matching documents then manipulate the array of objects to get the values you need. Use for of when iterating arrays not for in. Alternatively use map, filter, forEach and reduce array methods for manipulating array data.

What are developers their expectations when receiving a JSON response from a server

I have java library that runs webservices and these return a response in XML. The webservices all revolve around giving a list of details about items. Recently, changes were made to allow the services to return JSON by simply converting the XML to JSON. When looking at the responses, I saw they're not as easy to parse as I thought. For example, a webservice that returns details about items.
If there are no items, the returned JSON is as follows:
{"ItemResponse":""}
If there is 1 item, the response is as follows (now itemResponse has a object as value instead of a string):
{"ItemResponse":{"Items":{"Name":"Item1","Cost":"$5"}}}
If there two or more items, the response is (now items has an array as value instead of an object):
{"ItemResponse":{"Items":[{"Name":"Item1","Cost":"$5"},{"Name":"Item2","Cost":"$3"}]}}
To parse these you need several if/else which I think are clunky.
Would it be an improvement if the responses were:
0 items: []
1 item: [{"Name":"Item1","Cost":"$5"}]
2 items: [{"Name":"Item1","Cost":"$5"},{"Name":"Item2","Cost":"$3"}]
This way there is always an array, and it contains the itemdata. An extra wrapper object is possible:
0 items: {"Items":[]}
1 item: {"Items":[{"Name":"Item1","Cost":"$5"}]}
2 items: {"Items":[{"Name":"Item1","Cost":"$5"},{"Name":"Item2","Cost":"$3"}]}
I'm not experienced in JSON so my question is, if you were a developer having to use these webservices, how would you expect the JSON resonse to be formatted? Is it better to always return a consistent array, even if there are no items or is this usually not important? Or is an array not enough and do you really expect a wrapper object around the array?
What are conventions/standards regarding this?
Don't switch result types, always return an array if there are more items possible. Do not mix, for 1 item an object for more an array. That's not a good idea.
Another best practise is that you should version your API. Use something like yoursite.com/api/v1/endpoint. If you don't do this and you change the response of your API. All your client apps will break. So keep this in mind together with documentation. (I've seen this happen a lot in the past..)
As a developer I personally like your second approach, but again it's a preference. There is no standard for this.
There are several reasons to use json:
much more dense and compact: thus data sent is less
in javascript you can directly access those properties without parsing anything. this means you could convert it into an object read the attributes (often used for AJAX)
also in java you usually don't need to parse the json by yourself - there are several nice libs like www.json.org/java/index.html
if you need to know how json is build ... use google ... there tons of infos.
To your actual questions:
for webservices you often could choose between xml and json as a "consumer" try:
https://maps.googleapis.com/maps/api/place/textsearch/json
and
https://maps.googleapis.com/maps/api/place/textsearch/xml
there is no need to format json visually - is it not meant for reading like xml
if your response doesn't have a result, json-service often still is giving a response text - look again at the upper google map links - those are including a response status which makes sense as it is a service.
Nevertheless it's the question if it is worth converting from xml to json if there isn't a specific requirement. As Dieter mentioned: it depends on who is already using this service and how they are consumed ... which means the surrounding environment is very important.

JSON and changing datasets

I am fairly new to JSON and I want to create a choropleth example as so. http://gabrielflor.it/a-half-decade-of-rising-poverty Whenever the years are clicked it just goes to a different portion of the JSON (I'm assuming). Is this how functionality like this is usually done to avoid redrawing the whole map again and calling another JSON.js file? If so these .JSON files can get quite large?
Using a JSON is only a way to store values you need for each year. When you switch to another year the JS parse the JSON for the giving year and update the choropleth. For the example you have provided, here is the JSON used:
http://gabrielflor.it/static/data/saipe.json
This is a good way since you only have one JSON with every year you need and you load it only once. However since d3 needs datas this way I think you should add another JSON if you want to provide additional data like in gabrielflor example:
http://gabrielflor.it/static/js/d3.poverty-by-county.js?v=121107
He loads JSON like this with d3:
d3.json('../static/data/states.json', function (json) {
states = json;
});
or
d3.json('../static/data/saipehighlights.json', function (json) {
saipehighlights = json;
});
If you look at the network traffic for the example page you gave (ex. by using Chrome Developer Tools).
The file with the poverty data is quite large, but the mapping data file is even larger. You'll notice, that it takes longer for the website to load, but afterwords it runs very smoothly in the client without making any server calls.
The site is just about browsing information and nice design - for that purpose I think a longer load time is quite acceptable if the user experience after is smoother(i.e. user doesn't have to wait for year data to load).

Google Directions API, reduce XML output size

I'm using the Google Directions API specified here
https://developers.google.com/maps/documentation/directions/
it's working good, but the XML returned is a bit large, I don't need all the data they give.
the questions is, can I ask for a specific set of results?!
in my case, I want in "step" only "start_location" and "html_instructions".
You can't set a filter on resultset like you have mentioned above. If XML's size is too large , you can use JSON instead. Google recommends to use JSON format:
We recommend that you use json as the preferred output flag unless
your service requires xml for some reason. Processing XML trees
requires some care, so that you reference proper nodes and elements.
Reference: https://developers.google.com/maps/documentation/directions/#XML

hard time with JSON and google maps

It might be a frequent question but i cannot figure out how to prevent errors in my parsing when the script can't find a property...
in XML was easy because even the empty properties were like <location/>
but now if location is not available JSON paser cant find it and it results in errors...OR
it may happen the json has different property or a children lost its father..... so for instance if you need to extract the LocalityName is no more under SubAdministrativeArea but under AddressLine...
any of you have any experience about? what the best way to solve it and to parse it correctly?
While answering one of your other question I had written the following javascript code to obtain the lat and lng from the JSON returned by the maps api without any validations for zero results.
$.getJSON("getjson.php?address="+address,
function(data){
lat=data.results[0].geometry.location.lat;
lng=data.results[0].geometry.location.lng;
//.....map initialization code
}
);
Now if I were to validate for zero results, I'd modify the code in the following way
$.getJSON("getjson.php?address="+address,
function(data){
if (data.result.length>0) {
for (count=0;count<data.result.length;count++){
lat=data.results[count].geometry.location.lat;
lng=data.results[count].geometry.location.lng;
//.....map initialization code
}
}
}
);
As you can see parsing JSON comes naturally to javascript and to many other languages for it resolves down to arrays/lists and objects/dictionary/hash.
If I get this right and you are using a library to convert to json like gson try to construct some kind of object array like arrayList in java and then convert to json so every object you retrieve later in javascript is distinct and thus clear during debugging.Also if you don't use firebug give it try as it shows json data clearly.cheers