Last Updated:

Json Simple in Java Example: Parsing and Creating JSON

With this article, I'll start a series of publications on working with JSON in Java. To begin with, I will go through familiar and simple solutions, and then move on to advanced libraries. An example of "simple solutions" today would be working with Json Simple, namely parsing (parsing) an existing JSON object and creating a new one.

Json Simple API Overview

Json Simple is a simple API for processing JSON. The API itself consists of about 13 classes, which are based on the following 5 classes:

  1. The class is designed to parse a string with JSON content. It accepts an object or string.JSONParserjava.io.Reader
  2. The class is the Java representation of a JSON string. The JSONObject class inherits from HashMap and stores key-value pairs. It also has a very useful writeJSONString(Map map, Writer out) method, which is used to convert a map to a JSON string.JSONObject
  3. A class is a collection. It inherits the ArrayList and implements the interfaces and .JSONArrayJSONAwareJSONStreamAware
  4. JSONValue is a class for parsing JSON strings in Java objects. To do this, it uses the JSONParser class. JSONValue contains many useful methods for creating JSON from various types, for example, the writeJSONString(Object value, Writer out) method will create a JSON string from a Java object. It also has methods for working with special (control) characters using the escape(String s) method. With this method, you can safely work with quotation marks "" and characters such as \f, \t, \n, \, /, \r, \b, and many others.
  5. Interface. The class must implement this interface in order to be converted to JSON format.JSONAware

Json Simple API. Example of parsing a JSON string and creating a new JSON object

Below is an example of parsing a string with JSON content, namely the current weather in a city. I used the Openweathermap.org service to get weather data.

If you just want to try the JSON Simple API in action and don't want to bother with getting this data from some service, then just copy the resulting JSON from this link: openweather and use it as a regular string in Java.

Creating a maven project

For convenience, I created a simple maven project in Intellij IDEA and added the latest version of json-simple to the pom.xml file:

Now let's move on to creating the JsonUtils class, which will have all the necessary methods for parsing the finished json as a string and creating new Json content.

Now let's create a Main class to work with the utility parsing method and create json:

In the course of working with json Simple API for this example, you may have the following difficulties:

  • If the request for the weather service fails, replace the API key used in the example with your own.
  • If you have a problem with the maven project, you can create a simple Java project and add the Json Simple jar library manually.

Now let's run the application and look at the result of the execution in the console:

In this article, we got acquainted with the simple way of parsing/creating json, and in the following articles you will find other more convenient and popular ways to process json