Should response data be entirely reflected on the client side? - json

Recently I was involved in a discussion regarding parsing response data from a REST API.
My point was that only client necessary data should be parsed from JSON object. In my opinion there's no obvious reason to clutter a client object with information not needed for it's use case.
Other opinion stated that we should parse the response 1:1 so that it reflects received data entirely and than create some intermediate object for client usage only with the sufficient properties. In this case I don't see any obvious reason how this approach would benefit anything.
Are there any best practices regarding this topic? What are the pros/cons regarding each solution?

When you say "parsed" form a JSON object, what do you mean? JSON is an object representation... Are you making another object that looks like the JSON object for some reason but isn't the one generated by JSON.parse()?
Assuming that is what you are doing, then you should make that new object as focused as possible, perhaps as a ViewModel. There would be little point in creating ANOTHER JavaScript object that looks just like the results of JSON.parse().
If this is not what you are trying to accomplish, then I likely need more details to comment.

Related

Is it a good practice to use models in GetX Flutter to parse JSON data?

Hello there I am creating an application in Flutter and I am receiving JSON response from the API and I know that we need to parse the response to use in the Flutter app but I found that if we use the normal way as:
jsonData['key'] to get and show the data because this way you can handle any kind of response easily but when I am using the models way then I am facing a lot of issues in which the data structure and data types included.
and I think the model only provides an object structure in which you can access data as an object way like jsonData.key instead of the jsonData['key'] this is only my thinking you can correct me if I am wrong here.
I just want to know that if I am using a non-model way then will it affect my app or not?
models are not resiliant. Your code will always break if the api is modified.
Use an Object is a good practice because helps to take advantage of the strong typed language. This allows you to have a better debug process, catch potential error during writing time (and in compilation time). And this is independent of the state management package that you choose.
Firstly, this has nothing to do with getX. Parsing json into models is much cleaner. You can compare two objects but how do you compare two jsons?
And if you need to create an instance of the object, how would you do so without a model? How would you pass it to another class or a function? I think the answers to this questions will solve your dilemma.

Where shall I use protobuf

I was reading this article about protobuf and I wondered where to use it in the projects. I read some articles that said google created protobuf to replace XML, but as far as I know in 2008 (the first release) JSON was already there.
I searched more and I found an article that the writer suggested to use it instead of JSON, but I still don't get the idea completely.
So where shall I use it? Any special scenario, or like JSON whenever that I want to transport data? Any other scenarios?
It is useful whenever you want to serialize/deserialize your data. Typical situations include sending your data to someone else over the network, storing it to disk or keeping it in context while performing asynchronous processes.
Here is a brief explanation about the main differences between protocol buffer, json and XML: https://stackoverflow.com/a/14029040/6681872

Is it OK to just use POST method and JSON format for a REST-like API in Scala/Play

We decided to use POST method and JSON format for all of our internal APIs which makes everything simpler. But then we realized that this is not truly RESTful. More over it seems that GET requests are more lightweight than POSTs under high load.
We have a problem regarding GET methods. We have to bind our criteria object to the HTTP request (query string) which forces us to build Form object for each criteria model. As you know building the Form object will be done manually and there is no automation available like what we have for JSON formatters (Macro Inception).
Another issue is that we have to decide on whether to use route parameters or querystring.
I think it's simpler to use a single HTTP method and make all API calls uniform. Does it make sense?
POST is the method to be used for any operation that isn't standardized by the HTTP protocol, and simple retrieval is standardized in the GET method. So, using POST for simple retrieval isn't RESTful. More than that, it seems like you want to use POST so you can treat querystring parameters in the same way as the POST payload, but REST URIs are atomic identifiers, including the querystring. Your application shouldn't rely on URI semantics, and extracting bits of information that serve any purpose other than identification also doesn't make much sense in REST.
Frankly, from what you describe your API is so far from being considered truly RESTful that this shouldn't be a concern at all. Do whatever is more consistent with your tools and works better for your application. REST isn't for everyone, and worrying about designing an API that's truly RESTful when that isn't a requirement for your application is more likely to lead to bad design choices.
There's absolutely nothing wrong with using POST like you're describing. In fact, GET requests should not alter the state of the server but instead should only be used for retrieval. In other words, if you're sending data to the server to, for instance, create an entity, using GET would be technically incorrect.
There's nothing you're describing that sounds "not RESTful." POST can definitely be part of a RESTful architecture.
That said, the HTTP method you use should correspond to the action it will perform. For example, if you're retrieving an entity by ID, you should use GET whereas if you're updating an entity by ID, you should use POST or PUT. This gives developers using the API a hint as to the side effects and intended usage of the various API methods.

How to define and store data structure properties in a database

I'm working on a PHP web app with a Postgres backend. The app uses a variety of APIs and want to be able to add/edit the API endpoints used by the system dynamically.
I'm planning to handle variations in the API request URLs with replacement codes, for example: http://api.com/?key=%%api_key%%&user_id=%%user_id%%
The part I don't have a plan for is how to define and store the "shape" of the returned API data. For example, let's say I want to get a user's comments from different APIs. The structure of the data will likely differ from one to another. Even if they are all json data (vs. XML), the property(s) I care about will be located in different places. Is there an established way to do this?
I'm considering a text field with a json "map" to the location of the properties:
{
"user": {
"comments" : %%HERE%%
}
}
Presumably my app would parse this, and loop through it to find the indicated location and then use it to find the data in the corresponding location in the response data. But I'm not exactly how to do it or if this is even the best way. Any suggestions are welcome.
Thinking this through a bit more, I realize that an alternative approach would be to store some kind of algorithm to finding the data. Is there a precedent for this? I briefly considered the idea of storing raw PHP code that could be executed to parse the data, but this feels very wrong and potentially dangerous/insecure.
JOLT may be helpful. It's for transforming JSON to JSON, much like XSLT for XML. You could write a spec for each new api, which would transform the data into a uniform format for your app to read.

Application Input Specification: Drawing input data of method

Does anyone know a good way to to draw the exact structure of input data for a method? In my case I have to specify the correct input data for a server application. The server gets an http post with data. Because this data is a very complex json data structure, I want to draw this, so next developer can easily check the drawing and is able to understand, what data is needed for the http post. It would be nice if I can also draw http headers mark data as mandatory or nice to have.
I dont need a data flow diagramm or sth. like that. What I need is a drawing, how to build a valid json for the server method.
Please if anyone have an idea, just answer or comment this question, even if you just have ideas for buzz words, I can google myself.
In order to describe data structure consider (1) using the UML class diagram with multiplicities and ownership and "named association ends". Kirill Fakhroutdinov's examples uml-diagrams.org: Online Shopping and uml-diagrams.org: Sentinel HASP Licensing Domain illustrate what your drawing might look like.
As you need to specifically describe json structure then (2) Google: "json schema" to see how others approached the same problem.
Personally, besides providing the UML diagram I'd (3) consider writing a TypeScript definition file which actually can describe json structure including simple types, nested structures, optional parts etc. and moreover the next developer can validate examples of data structures (unit tests) against the definition by writing a simple TypeScript script and trying to compile it