Inconsistent response (JSON array lengths different) from Google Places API - json

When I make GET HTTP call to Google Places API. (Example Call: "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&types=food&name=harbour&sensor=false&key=") ....
Under "results", inconsistent number of elements are returned. What is the base list of fields that will always be returned?
In the screenshot below, you can see how the array length count under "results" is different, counts are 11, 10, 10, and 8.
Screenshot below shows how one the JSON Arrays in the response has more elements than the other and the extra fields are circled in red.
I am confused here because I am trying to write code to parse the JSON and trying to understand which are the fields to expect? Missing fields in some arrays in the response is breaking my code.
Am I interpreting the API documentation incorrectly or Is this a bug that I should be filing with Google directly?

You might consider using logic that does not require any fields at all. Because the server is out of your control, it could return anything at any time (due to API changes, bugs, technical faults, etc.)
For example, in Sprockets I loop through whatever objects are returned and if the name matches something that I expect, then I process it. Otherwise the fields in my objects simply remain empty. You can see the top-level processing Java code in the PlacesResponse constructor.

It's not a bug, but documented behaviour. The Google Places API documentation says:
Each result within the results array may contain the following fields:
In other words, Google returns ratings, opening hours etc for places if it has them, but it will leave them out if it does not or the field is not relevant to the place (eg. you wouldn't have opening hours for an apartment complex or rate a train station). As #pushbit says, you have to parse accordingly and check whether each field was returned.

Related

JSON object losing info between API call and Frontend

I'm making a website that gets its info from a RESTapi I've written and hosted myself, have had no data problems thus far.
Now I'm trying a simple retrieve of a json object and I get all the info correctly as shown here in the API. (Visualized & tested in Swagger)
As you can clearly see, it retrieves it the complete object and underlying objects correctly (blurred sensitive info).
Pay attention to the property "AmountOfEggs" though.
Now when i call this api endpoint (exactly the same way) in my site and console.log the result.data, this is the feedback.
Now for some reason I can't recieve AmountOfEggs in my frontend.
I've recreated the entire object, wrote different names, passed different props (Id, NumberBus, etc are passed in this situation with no problem, which are also int (number) types).
I have no idea why this property gets "dropped" in the transfer and filled with/defaults to an empty string.
Any help would be greatly appreciated.
I found where it went wrong and it's due to different factors.
To start, I am making this site using the Vue framework, which has reactive components, which means, data gets adjusted and you get live updated components on your views/pages.
In the page that contained the call, I also wanted to add dynamic styling. The code I used for this is the following:
v-for="seller in retrievedSellers"
:key="seller.Id"
:class="[
'sellerStyle'
, (seller.AmountOfEggs = 0 ? 'grey-out-seller' : ''),
]"
Now two things to note, first of all this might look like backend coding but this is Vue logic on the .vue file which handles the dynamic options that Vue provides so I never thought to look here for the error.
The error of couse is the 'seller.AmountOfEggs = 0' part, using one equal sign (assignment) instead of two (comparison).
So I learned,
Vue doesn't throw errors on assignments in code that is used to generate frontend (where you should NEVER alter your data).
My console.log was the moment the response of the API came in, where apparently it was already assigned a new value (due to my code above) before hitting the console.log.
It still greatly surprises me that Vue handles ^this^ first to make sure your reactive components are up to date before finishing the api call itself.
However, I still don't understand how it said "" instead of 0.
I'm using typescript (javascript strongly-typed) so it definitely wants to contain a number and not an empty string. Even in the declaration of my DTO the property is defined as (and expects) a number.

Web API GET method with two complex parameters

I have a Web API program that has 8 get methods.
Each method can get 2 parameters - filters and sorts.
filters - a json object that contains all the params you can filter by
sorts - an array that contains all the properties you want to sort by
For example:
www.facebook.com/GetFriends?filters={name:"lior", color:{name:"blue", RGB:{255, 100, 0}}}&sorts=[{prop:"name", desc:true}, {prop:"age", desc:false}]
The api's only available from other servers, so there's no need to worry about URL characters limit in browsers.
We chose GET method because the purpose of this api is only to get data.
I'm not sure the url we require is in a 'normal' format-
Is that okay in a GET method to use a two query params that one of them is json and the other is array?
Thank you for your help

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.

CF9 & Solr: CFSEARCH vs CFHTTP

I am working in a CF 9 environment with Solr collections. I have 7 of them that I'm working with, all are strictly PDFs. Using CFSEARCH, I'm not getting all of the documents that should be appearing in the results.
To give a specific example, the client has ten PDFs that contain the string 1386 somewhere in the body of the documents. But when using the search form and entering 1386, only 4 of them appear. The client is concerned that not all PDFs with 1386 are being displayed in search results.
I have been following (with great interest) David Faber's posts espousing the CFHTTP method of querying a Solr collection, but I'm running into snags trying to implement it.
One of the issues is that when using CFSEARCH, I'm using all four CUSTOM fields, and I'm also getting CONTEXT which will highlight the keyword. In the CFHTTP method, I'm not getting CONTEXT with highlighted keywords.
Also, I'm trying to deserialize JSON and convert that to a query object. But I keep getting the common error message about
attempting to reference a scalar variable array as a structure with members
Advice/suggestions greatly appreciated.