JSON & Java — A Beginner-Friendly Guide

JSON & Java — A Beginner-Friendly Guide

Modern applications constantly exchange data between browsers, servers, and mobile apps. To make this communication simple and universal, we use a lightweight data format called JSON.

This blog explains:

  • What is JSON?

  • Why JSON is so popular

  • How JavaScript uses JSON

  • How Java interacts with JSON

  • How to create and read JSON objects (with examples)


1. What is JSON?

JSON (JavaScript Object Notation) is a lightweight, text-based format for storing and exchanging data.

It is:

  • Human readable

  • Language independent

  • Easy for machines to parse

  • Universally supported

✔ JSON represents data using:

  • Key–value pairs

  • Arrays

  • Nested objects

✔ JSON is widely used in:

  • REST APIs

  • Web applications

  • Mobile apps

  • Server-to-server communication

  • Configuration files


2. Why JSON Is So Popular

JSON became the standard for data exchange because:

FeatureWhy It Matters
LightweightSmaller size → faster communication
ReadableEasy for humans and developers
Language-freeWorks in JavaScript, Java, Python, Go, etc.
Native in JavaScriptPerfect for web browsers
Easy to parseSimple tools available in all languages

3. JSON in JavaScript

JSON was originally inspired by JavaScript object syntax.
In fact, JavaScript treats JSON almost like a native object.

Example:

var person = { "name": "Vinod", "address": "Bangalore", "designation": "Open source Publisher", "employer": "Self Business" };

Accessing values:

console.log(person.name); console.log(person.address);

4. JSON in Java

Java uses libraries to parse and create JSON because Java is strictly typed.

Popular Java libraries:

  • Jackson

  • Gson

  • JSON.simple

  • Org.JSON

Typical Java JSON operations:

➤ Convert JSON string → Java Object

➤ Convert Java Object → JSON string

➤ Read fields from JSON

➤ Create nested JSON structures

Example in Java using Jackson:

ObjectMapper mapper = new ObjectMapper(); String json = "{\"name\":\"Vinod\", \"address\":\"Bangalore\"}"; Person person = mapper.readValue(json, Person.class); System.out.println(person.getName());

5. JSON Connecting Java ↔ JavaScript

✔ Browser → Server → Browser flow

  1. JavaScript creates JSON

  2. Sends JSON to Java backend using REST API

  3. Java backend processes JSON

  4. Java sends JSON response back

  5. JavaScript reads JSON and updates UI

This is the core flow of all modern websites.


6. Creating JSON in JavaScript (Full Example)

Here is your example cleaned and formatted for blog readers:


📄 Example: Creating & Displaying a JSON Object in JavaScript

<html> <head> <title>Creating JSON Object</title> <script> // Creating a JSON object var jsonObject = { "address": "Bangalore", "designation": "Open source Publisher", "employer": "Self Business", "name": "Vinod" }; document.write("<h1>JSON Object Details</h1>"); document.write("<h3>Name: " + jsonObject.name + "</h3>"); document.write("<h3>Address: " + jsonObject.address + "</h3>"); document.write("<h3>Designation: " + jsonObject.designation + "</h3>"); document.write("<h3>Employer: " + jsonObject.employer + "</h3>"); </script> </head> <body> </body> </html>

Output

JSON Object Details Name: Vinod Address: Bangalore Designation: Open source Publisher Employer: Self Business

7. Convert JSON ↔ String in JavaScript

Convert object → JSON string:

var jsonStr = JSON.stringify(jsonObject); console.log(jsonStr);

Convert JSON string → object:

var obj = JSON.parse(jsonStr); console.log(obj.name);

8. Java + JSON Example

Java object to JSON:

ObjectMapper mapper = new ObjectMapper(); Person p = new Person("Vinod", "Bangalore"); String json = mapper.writeValueAsString(p); System.out.println(json);

JSON to Java object:

String json = "{\"name\":\"Vinod\"}"; Person p = mapper.readValue(json, Person.class);

9. Summary

JSON is the most popular format for data exchange because:

  • It is simple and readable

  • Works across all languages

  • JavaScript handles it naturally

  • Java has rich libraries to parse and generate JSON

Whether you're building a web application, mobile app, or backend system, JSON is at the heart of modern development.


No comments:

Post a Comment

12 classic String-based Java interview questions with simple explanations and code.

  1️⃣ Check if a String is a Palindrome Problem Given a string, check if it reads the same forward and backward. Example: "madam...

Featured Posts