JSON Response Format Example [JavaScript/AJAX Code]

For the server to respond in JSON format, include the JSON data in the response message body and specify the "Content-Type: application/json" HTTP header. The server may also set the Content-Length header to indicate the length of the JSON data in the response. If the Content-Length HTTP header is not set, then the HTTP/1.0 server closes the stream after sending the JSON data, and the HTTP/1.1 server sends the JSON data in a chunked transfer encoding format. In this JSON Response Format example, we send a request to the ReqBin echo URL to get JSON Response from the server. Click Send to execute the JSON Response Format example online and see the results. The JavaScript/AJAX code was automatically generated for the JSON Response Format example.
JSON Response Format Example [JavaScript/AJAX Code] Send
GET /echo/get/json HTTP/1.1
Host: reqbin.com

Updated: Viewed: 8455 times
JavaScript/AJAX code for JSON Response Format example

JavaScript/AJAX code for JSON Response Format Example

This JavaScript/AJAX code snippet was generated automatically for the JSON Response Format example.
<< Back to the JSON Response Format example

What is JSON?

JavaScript Object Notation (JSON) is a language-neutral, text-based data interchange format that defines a few basic formatting rules for encoding structured data. The JSON format represents four primitive types (strings, numbers, booleans, and null) and two structured types (objects and arrays). A JSON file is used to exchange data between applications written in many programming languages, including Java, JavaScript, C++, C #, Go, Python, PHP, etc.

JSON Example
{
"Id": 78912,
"Customer": "Jason Sweet",
"Quantity": 1,
"Price": 18.00
}

Why specify the correct content type for JSON response?

Each HTTP resource has a media type, or MIME type, which describes the resource type and allows browsers and servers to understand it properly. As an example, if the server is capable of accepting both JSON and XML content types on one endpoint, a "Content-Type: application/json" will tell the server to interpret the body data as JSON, whereas a "Content-Type: application/xml" will tell the server to interpret the body data as XML.

JSON Response Format Example

To get the response format, we first need to send a request to the ReqBin echo URL:

JSON Request Example
GET /echo/get/json HTTP/1.1
Host: reqbin.com

The server returning our JSON Response Format:

JSON Response Format Example
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 19

{"success":"true"}

See also

Generate code snippets for JavaScript/AJAX and other programming languages

Convert your JSON Response Format request to the PHP, JavaScript/AJAX, Curl/Bash, Python, Java, C#/.NET code snippets using the JavaScript/AJAX code generator.