Pass JSON object vs JSON string in HTTP POST - json

I'm building a REST API in JAVA and C# and I was wondering about the way I should pass data to those services.
What I'm familiar with as the right way is to send JSON object as the data in the POST body:
{name:'Dor'}
but I can also pass a string and parse the JSON in my service:
'{name:'Dor'}'
What is the preferable way from performance factor? or any other factors?

Basically, if you need to send the json data across via jquery, then we need to use stringify, else the data would be serialized into to key=value pair.
So, you cannot send the json object directly via jquery ajax method.
How it works behind the hood:
In $.ajax function, if we provide data as
data :{key1:"value1", key2:"value2"}
is serialized to key1=value1&key2=value2
if we provide data as
data :'{key1:"value1", key2:"value2"}' or JSON.stringify({key1:"value1", key2:"value2"})
is sent as {key1:"value1", key2:"value2"}
So, what we can conclude is that, we cannot pass json object directly via jquery, we can send only json string. Hope this clarifies everyone.

Related

Create JSON Object in React.js from Rest Service

I am looking for either guidance or a good example where I can map data coming from rest services to JSON "type" object which can then be used in a number of different react components.
The JSON Object will be used to map data from a few different rest services, which essentially hold very similar data which makes it better to use one object and then to bind the data to the respective React Components.
I am fairly new to React.JS and I have googled around to find a data mapper to JSON from Rest Service example.
Can anyone help?
You typically don't have to do too much, at least on the front end side. As long as the REST endpoint can return JSON responses you'll be fine. Just make sure you set the appropriate Content-Type headers in the request. Note that setting the header doesn't guarantee a JSON response, the server has to be able to send it in that format.
If you're creating the REST service yourself, you have many options. If you're using node, you can simply return a javascript object. If you're using some other language like Java, C#, etc., they come with libraries that can serialize objects into JSON for you. I use JSON.net when working with C#. In these cases, because the data will be returned as a string, you'll just need to JSON.parse() it upon receiving it and then set it to the appropriate React component's state.

How to get JSON output from Cloudant CouchDB?

I'm new to cloudant and still figuring out various APIs that it provides for access and retrieval.
So far, I've used findbyIndex using selector string which returns JSON as de-serialised object of the POJO supplied.
For a certain use case which I'm working- I need to get JSON only and not the de-seralised object. Is there any way or API from which I can get the actual cloudant doc as JSON only ?
If you know the ID of the document you want to read only the raw JSON, you can use the find method to get the raw inputstream from the request. Currently using the findByIndex method you can only get a deserialised java object back, so you could always use a Map object for your POJO. Although that being said it might be possible to pass the type String for the class to deserialise, however I have not tested this.

Using Rest assured to validate saved JSON response

I have a question regarding REST Assured. - https://code.google.com/p/rest-assured/wiki/Usage
I understand that I can use REST assured to make HTTP calls(.get .put etc.) and validate the response using when() etc. I would like to validate JSON responses that I have already saved in the database, instead of Calling the web service realtime and validating it's response.
Can I use REST-assured to Load a JSON response as a String and validate it?
Yes you can use Rest Assured's JsonPath project independently of Rest Assured (see getting started page). Once you have it in classpath you can do something like this:
JsonPath jsonPath = new JsonPath(<your json as string>);
String title = jsonPath.getString("x.y.title");

How to send JSON data in an Ajax request?

what would be the best way to send string (unicode text) & array-of-string data (several hundreds characters, for now formatted as a single Json object) from an HTML page to a PHP server document (to be then recorded in a db)? I'm pretty much new at Json and I'm not sure how to send a Json object server side with an Ajax call.
Right now, my idea would be to somehow serialize my Json object into a string, then send that string as a parameter to a POST Ajax call, but again, I'm not sure if this is the right way or how to serialize Json.
I don't use jquery at this time.
You can use JSON.stringify as you suggested.
JSON.stringify
But why do you want to serialize as JSON on the client side as opposed to sending as is to the server and converting to JSON on the server side? Then you could use a JSON library in whatever language your server supports.

How to return JSON from a servlet based on request parameters

How do I use JSON in java servlet? Using the URL, to pass parameters from a POST servlet, the JSON would respond based on the URL parameters.
You need a JSON library in Java. With that library, you will be able to serialize Java Objects into JSON objects, and send them through HttpServletResponse instance in your Servlet.
"How do you use it"?
JSON is simply one way of representing hierarchical data (such an an object model) in a flat text format (such as an HTTP body, or filesystem file). So you'd use it to represent hierarchical data in these situations.
If you mean how do you parse/create it, there are many mature libraries for JSON handling.
Perhaps a specific situation or use case would assist in pinning down your question, if the above is not the answer you were looking for.