top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Send and Receive JSON Data to and from Server

Introduction:

JSON is a text-based data format following JavaScript object syntax, which was popularized by Douglas Crockford. Even though it closely resembles JavaScript object literal syntax, it can be used independently from JavaScript, and many programming environments feature the ability to read (parse) and generate JSON.

JSON exists as a string — useful when you want to transmit data across a network. It needs to be converted to a native JavaScript object when you want to access the data. This is not a big issue — JavaScript provides a global JSON object that has methods available for converting between the two.The JSON syntax is like JavaScript's object literal syntax, except that it cannot be assigned to a variable. JSON merely represents the data itself. Since JSON is just a string of text, it needs to be converted to an object to be useful inside JavaScript. Likewise, JavaScript objects need to be converted into strings in order to be used as JSON data.

What JSON looks like

JSON is a human-readable format for storing and transmitting data. As the name implies, it was originally developed for JavaScript, but can be used in any language and is very popular in web applications. The basic structure is built from one or more keys and values:


{

"key": value

}

JSON doesn’t have to have only key:value pairs; the specification allows to any value to be passed without a key. However, almost all of the JSON objects that you see will contain key:value pairs. You’ll often see a collection of key:value pairs enclosed in brackets described as a JSON object. While the key is any string, the value can be a string, number, array, additional object, or the literals, false, true and null. For example, the following is valid JSON:

{

"key": "String",

"Number": 1,

"array": [1,2,3],

"nested": {

"literals": true

}

} JSON doesn’t have to have only key:value pairs; the specification allows to any value to be passed without a key. However, almost all of the JSON objects that you see will contain key:value pairs.Two functions for working with JSON are built into JavaScript:

  • JSON.parse() - Converts a JSON string into a JavaScript object.

  • JSON.stringify() - Converts a JavaScript object into a JSON string.

The following explains how to send data between the browser and server with JSON.


Send JSON Data from the Client Side

  1. Create a JavaScript object using the standard or literal syntax.

  2. Use JSON.stringify() to convert the JavaScript object into a JSON string.

  3. Send the URL-encoded JSON string to the server as part of the HTTP Request. This can be done using the HEAD, GET, or POST method by assigning the JSON string to a variable. It can also be sent as raw text using the POST method, but this may create extra work for you on the server-side.

Receive JSON Data on the Server Side

  1. Convert the incoming JSON string to an object using a JSON parser for the language of your choice. At json.org., you'll find JSON parsers for many modern programming languages. The methods available depend upon which parser you are using. See the parser's documentation for details.

  2. Do whatever you wish with the object.

Send JSON Data from the Server Side

  1. Create a new object for storing the response data.

  2. Convert the new object to a string using your JSON parser.

  3. Send the JSON string back to the client as the response body (e.g, Response.Write(strJSON), echo $strJSON, out.write(strJSON), etc.).

Receive JSON Data on the Client Side

  1. Convert the incoming JSON string to an object using JSON.parse().

  2. Do whatever you wish with the object.

Posting JSON to the Server

To post JSON data to the server, you need to provide the JSON data in the HTTP POST request body and pass the "Content-Type: application/json" request header. The Content-Type request header specifies the media type for the resource in the body. Additionally, you can pass an "Accept: application/json" header, which tells the server that the client is expecting JSON data. In this POST JSON example, we send JSON data to the ReqBin echo URL with the appropriate Accept and Content-Type HTTP headers. Click Send to execute the POST JSON example online and see the results..

Example-1

Posting JSON to the Server


POST /echo/post/json

HTTP/1.1 Host: reqbin.com

Accept: application/json

Content-Type: application/json

Content-Length: 81

{

"Id": 78912,

"Customer": "Jason Sweet",

"Quantity": 1,

"Price": 18.00

}


Example-2

The following is an example of sending JSON data to ReqBin echo URL


POST JSON Request


POST /echo/post/json HTTP/1.1

Host: reqbin.com

Accept: application/json

Content-Type: application/json

Content-Length: 81

{

"Id": 78912,

"Customer": "Jason Sweet",

"Quantity": 1,

"Price": 18.00

}


The server response to our POST JSON request:


POST JSON Response

HTTP/1.1 200 OK

Content-Type: application/json

Content-Length: 19

{

"success":"true"

}


  1. JSON is purely a string with a specified data format — it contains only properties, no methods.

  2. JSON requires double quotes to be used around strings and property names. Single quotes are not valid other than surrounding the entire JSON string.

  3. Even a single misplaced comma or colon can cause a JSON file to go wrong, and not work. You should be careful to validate any data you are attempting to use (although computer-generated JSON is less likely to include errors, as long as the generator program is working correctly). You can validate JSON using an application like JSONLint.

  4. JSON can actually take the form of any data type that is valid for inclusion inside JSON, not just arrays or objects. So for example, a single string or number would be valid JSON.

  5. Unlike in JavaScript code in which object properties may be unquoted, in JSON only quoted strings may be used as properties.

Conclusion

JSON has come to be one of the most popular standards for data interchange, being easy for humans to read while being lightweight to ensure small transmission size. Its success has also been caused by it being equivalent to JavaScript objects, making it simple to process in web frontends. However, JSON isn’t the solution for everything, and alternate standards like YAML are more popular for things like configuration files, so it’s important to consider your purpose before choosing.


9,573 views1 comment

Recent Posts

See All

Exception Handling in Selenium Webdriver

What is an exception? An exception is an error that occurs during the execution of a program. However, while running a program, programming languages generate an exception that must be handled to prev

1 Comment

Rated 0 out of 5 stars.
No ratings yet

Add a rating
Guest
Mar 02
Rated 4 out of 5 stars.

sus

Like
bottom of page