Home » Core Java » json » JSON Example

About Odysseas Mourtzoukos

Avatar photo
Mourtzoukos Odysseas is studying to become a software engineer, at Harokopio University of Athens. Along with his studies, he is getting involved with different projects on gaming development and web applications. He is looking forward to sharing his knowledge and experience with the world.

JSON Example

In this article, we will explain JSON using examples.

1. What is JSON

JSON is an acronym for JavaScript Object Notation. It supports data structures like objects and arrays, so, it is easy to write and read data from JSON. It is a language-independent data format. It supports almost every kind of language, framework, and library.

JSON Example

2. Why do we use it

JSON is a data format interchange – a way of storing and transferring data. Alongside server-browser communication, it’s common to see uses such as database migration (e.g. converting JSON to SQL) and exporting data from proprietary web apps. It’s used by lots of APIs and Databases, and it’s easy for both humans and machines to read. JSON represents objects as name/value pairs, just like a Python dictionary.

3. History of JSON

In the early 2000s, JSON was initially specified by Douglas Crockford. In 2013, JSON was standardized as ECMA-404, and RCF 8259 was published in 2017. It’s derived from a subset of JavaScript and came about specifically when object literals and array literals were added to the JavaScript language. Unlike many technologies, JSON wasn’t really ‘invented’ by just one person. Many people discovered this data transfer and storage use independently of one another. In the early 2000s when people continued to discover and learn about this technique, Crockford was definitely instrumental in the widespread application of JSON we see today. It began in 2002 when he registered the domain name json.org while working at State Software. Using his high-profile status, and subsequent roles at dotcom tech giants such as PayPal, he spread the word about JSON. Then sometime in 2005 the advent of Single Page Applications and modern mobile/web apps that we know today needed some kind of data interchange to function seamlessly. This caused JSON to explode in popularity.

4. Features of JSON

  • JSON is Scalable. Because of language-independent, it works with most of the modern programming language.
  • JSON is lightweight.
  • JSON is easy to read and write.
  • JSON is a text-based, human-readable data exchange format.

5.1 JSON Formatter

JsonFormatter helps to

  • Format/ Beautify JSON
  • Validate JSON
  • Minify JSON
  • Convert JSON to XML, YAML, CSV

5.2 Quicktype

Quicktype is another great online tool that helps to convert JSON to classes/structs in any one of the following programming languages. It supports

  • Go
  • Java
  • Python
  • Ruby
  • Swift
  • Objective-C
  • and many more…

5.3 JSON Pretty Printer

JSON Pretty Printer utility prints JSON data in a legible, indented format. It provides the most benefit to people who are used to the command line. This tool is pretty useful for developers building or consuming JSON-based APIs.

5.4 ijson

Originally developed in 2016, ijson is an iterative JSON parser with a standard Python iterator interface. The most common usage is having ijson yield native Python objects from a JSON stream that is under a prefix. ijson offers several implementations of the actual parsing in the form of C-based YAJL (Yet Another JSON Library) or Python back-ends.

6. Pros and Cons of JSON

Pros

  • JSON’s structure directly corresponds to basic data types of most of script languages such as array and map.
  • JSON has distinction between string, number and boolean. The distinction saves a bit of programmer’s work to convert to appropriate type manually.
  • Easy to read/write/parse.
  • Reasonably succinct.
  • Common “standard” with many libraries available.

Cons

  • JSON doesn’t have a “Date” type.
  • JSON isn’t efficient over the wire, binary protocols are better.
  • JSON doesn’t distinguish between floating point and decimal.
  • JSON doesn’t have a binary type, base64 is commonly used as a workaround (much like XML), but there’s no standard annotation that differentiates it from a normal string.
  • Can’t use comments.
  • JSON is not the fastest.

7. Simple Java Example

In this example, we will use a library called json.simple . We will create a simple JSON object with some properties and we will parse a JSON file to get its contents.Example.java

package code;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class Example {

	public static void main(String[] args) {

		JSONObject obj = new JSONObject();

		obj.put("name", "foo");
		obj.put("num", new Integer(100));
		obj.put("balance", new Double(1000.21));
		obj.put("is_vip", new Boolean(true));

		System.out.println(obj);

		JSONParser jsonParser = new JSONParser();
		try (FileReader reader = new FileReader("test.json")) {
			// Read JSON file
			obj = (JSONObject) jsonParser.parse(reader);
			System.out.println(obj);

			// JSONArray employeeList = (JSONArray) obj;
			// System.out.println(employeeList);

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
}
Fig. 1: Simple Java Example output.
Fig. 1: Simple Java Example output.

8. Summary

In this article, we explained what is JSON creating an example. It is a lightweight format that enables you to easily share, store, and work with data. As a format, JSON has been experiencing increased support in APIs, including the Twitter API. Because you will be likely procuring them from other sources, it is important to think about how to get the best out of JSON in your programs.

Check our JSON tutorials to learn more.

9. Download the Source Code

This was an example of JSON in Java.

Download
You can download the full source code of this example here: JSON Example

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions

 

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments